【问题标题】:Pandas boolean indexing w/ Column boolean array带有列布尔数组的 Pandas 布尔索引
【发布时间】:2020-04-19 13:39:31
【问题描述】:

数据集:我正在处理的数据框的名称是“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

我看到了 index-wise(row-wise) 布尔索引示例,但找不到按列的布尔索引。 任何人都可以帮助如何修复此代码?

提前谢谢你。

【问题讨论】:

    标签: pandas boolean


    【解决方案1】:

    使用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)
    

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多