带列布尔数组的熊猫布尔索引

时间:2020-04-19 13:39:31

标签: pandas boolean

数据集:我正在处理的数据框的名称为'f500'。这里是 first five rows in the dataframe

目标:选择仅包含数值的数据


我尝试过的事情

1)我尝试使用布尔数组过滤掉非数字值,并且没有错误。

numeric_only_bool = (f500.dtypes != object)

boolean array

2)但是,当我尝试对该布尔数组进行索引时,会发生错误。

numeric_only = f500[:, numeric_only_bool]

error message

我看到了按索引(行)布尔索引的示例,但是找不到按列的布尔索引。 谁能帮忙解决此代码?

谢谢。

1 个答案:

答案 0 :(得分:2)

使用DataFrame.loc

numeric_only = f500.loc[:, numeric_only_bool]

另一个与DataFrame.select_dtypes的解决方案:

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