jaeseongpark
jaeseongpark

Reputation: 29

Pandas boolean indexing w/ Column boolean array

Dataset: The name of dataframe I am working on is 'f500'. Here is the first five rows in the dataframe

Goal: Select data with only numeric value


What I've tried:

1) I tried to use boolean array to filter out the non-numeric values and there was no error.

numeric_only_bool = (f500.dtypes != object)

boolean array

2) However, when I tried to do indexing with that boolean array, an error occurs.

numeric_only = f500[:, numeric_only_bool]

error message

I saw index-wise(row-wise) boolean indexing examples but could not find column-wise boolean indexing. Can anyone help how to fix this code?

Thank you in advance.

Upvotes: 1

Views: 113

Answers (1)

jezrael
jezrael

Reputation: 862406

Use DataFrame.loc:

numeric_only = f500.loc[:, numeric_only_bool]

Another soluion with DataFrame.select_dtypes:

#only numeric
numeric_only = f500.select_dtypes(np.number)
#exclude object columns
numeric_only = f500.select_dtypes(exclude=object)

Upvotes: 2

Related Questions