0 votes
1 view
in Python by (10.8k points)

This is my code:

df = pd.DataFrame({'First':[1,2,3,4],'Second':[1,2,3,'string'],'Third':[1,2,3,4],'Fourth':['string','lava','cake','Volcano']})

columns = df.applymap(np.isreal).all()

print(type(columns))

print(columns)

True_columns = []

False_columns = []

The output is:

<class 'pandas.core.series.Series'>

First      True

Second    False

Third      True

Fourth    False

dtype: bool

I need to save all the columns with TRUE (First, Third) in True_columns list and all the FALSE (Second, Fourth) in False_columns list. I am learning pandas. Could anyone help me how to achieve this requirement ?

Thanks in advance.

1 Answer

0 votes
by (21.4k points)

You can do it very easily by doing this:

True_columns = columns[columns].index.tolist() 

False_columns = columns[~columns].index.tolist()

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...