筛选不在列表中的panda数据帧列名



我有以下数据帧:

df = pd.DataFrame(
{'a': [-0.558, -0.5584, -0.5583, -0.5583, -0.5581], 
'b': [0.5324, 0.5324, 0.5324, 0.5324, 0.5323], 
'c': [-0.2803, -0.2803, 0.2803, -0.2803, -0.2851],
'd': [0.2359, 0.6532, 0.1254, 0.1231, 0.1785]})
a        b       c      d
0   -0.5580 0.5324  -0.2803 0.2359
1   -0.5584 0.5324  -0.2803 0.6532
2   -0.5583 0.5324  -0.2803 0.1254
3   -0.5583 0.5324  -0.2803 0.1231
4   -0.5581 0.5323  -0.2851 0.1785

我想创建两个具有列名的列表,其中一个列表包含具有负值的列名,另一个列表则包含具有正值的列名。

negative = df.columns[(df < 0).any()]

我很难得到这样的东西:

positive = df.columns NOT IN NEGATIVE

此代码返回错误:

list(df[[column for column in df.columns if column != negative]])
>>> ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

您可以使用布尔索引:

mask = (df < 0).all()
negative = df.columns[mask].tolist()
not_in_negative = df.columns[~mask].tolist()
print(negative)
print(not_in_negative)

打印:

['a', 'c']
['b', 'd']

最新更新