Reputation: 29
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)
2) However, when I tried to do indexing with that boolean array, an error occurs.
numeric_only = f500[:, numeric_only_bool]
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
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