I have the following dataframe:
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
I want to create two lists with column names where one list contains column names with negative values and another one with column names with positive values.
negative = df.columns[(df < 0).any()]
I'm struggling to get something like:
positive = df.columns NOT IN NEGATIVE
This code return error:
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()
Any suggestions are much appreciated! Thank you!
You can use boolean indexing:
mask = (df < 0).all()
negative = df.columns[mask].tolist()
not_in_negative = df.columns[~mask].tolist()
print(negative)
print(not_in_negative)
Prints:
['a', 'c']
['b', 'd']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.