【问题标题】:"ValueError: Boolean array expected for the condition, not float64" when slicing dataframe by condition“ValueError:条件需要的布尔数组,而不是float64”在按条件切片数据帧时
【发布时间】:2021-03-31 07:47:27
【问题描述】:

我有很多列的数据框,我想创建一个表,其中包含一个一致的列和两个在每个循环中都在变化的列。

问题是切片的第一步失败了,我无法创建这个新的 3 columnns 数据框。

#list of pairs of the columns i'm interested in for each loop
cols_of_interest=[['col1', 'col2'],['col3','col4']]


for i in cols_of_interest:
    table=df[df[['col_not_from_list',i[0],i[1]]]]
    table = table.dropna(how='any',axis=0)
...

----> 7 表=df[df[['col_not_from_list',i[0],i[1]]]]

ValueError:条件需要的布尔数组,而不是 float64

较早的帖子也提到了同样的错误,提到熊猫的版本可能是问题,但我不认为是这种情况(因为这些帖子是几年前的)。此外,我发现了使用 mask 的提示,但我不明白为什么以这种方式切片它不起作用。

我的最终目标:在循环内创建包含三列的数据框:一致的 + 列表中的另外两个。

【问题讨论】:

    标签: python pandas dataframe slice


    【解决方案1】:

    改成

    cols_of_interest=[['col1', 'col2'],['col3','col4']]
    
    
    for i in cols_of_interest:
        table = df[['col_not_from_list']+i]
        table = table.dropna(how='any',axis=0)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-25
      • 2022-12-23
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 2019-11-27
      • 2016-03-21
      • 2013-04-17
      相关资源
      最近更新 更多