"ValueError: Boolean array expected for the condition, not float64" when slicing dataframe by condition

"ValueError: Boolean array expected for the condition, not float64" when slicing dataframe by condition

我有包含许多列的数据框,我想创建 table 具有一个一致的列和另外两个在每个循环中都在变化的列。

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

#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 table=df[df[['col_not_from_list',i[0],i[1]]]]

ValueError: Boolean array expected for the condition, not float64

较早的帖子认为同样的错误提到 pandas 的版本可能是问题,但我不认为是这种情况(因为这些帖子来自 mserveral 几年前)。此外,我发现了使用 mask 代替的技巧,但我不明白为什么这样切片不起作用。

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

改为

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)