[英]New column based on another multiple dataframe columns
Below is my dataframe.下面是我的数据框。
a![]() |
b ![]() |
---|---|
12 ![]() |
0 ![]() |
1 ![]() |
21 ![]() |
0 ![]() |
0 ![]() |
Now want to add a column name 'c' which return yes when a or b is non-zero and return no when a and b is zero现在要添加一个列名“c”,当 a 或 b 为非零时返回 yes,当 a 和 b 为零时返回 no
a![]() |
b ![]() |
c ![]() |
---|---|---|
12 ![]() |
0 ![]() |
yes![]() |
1 ![]() |
21 ![]() |
yes![]() |
0 ![]() |
0 ![]() |
no![]() |
If need test all columns compare by 0
and test if all values per row by DataFrame.all
with set yes
, 'no' by numpy.where
:如果需要测试所有列比较
0
并通过DataFrame.all
测试每行的所有值是否设置yes
, 'no' 通过numpy.where
:
df['c'] = np.where(df.eq(0).all(axis=1), 'no','yes')
print (df)
a b c
0 12 0 yes
1 1 21 yes
2 0 0 no
Another idea is test if at leastone value is not 0
by DataFrame.any
, then is used mapping:另一个想法是通过
DataFrame.any
测试是否至少一个值不为0
,然后使用映射:
df['c'] = df.ne(0).any(axis=1).map({False: 'no',True:'yes'})
If possible multiple columns and need test only a,b
columns:如果可能的话,多列并且只需要测试
a,b
列:
cols = ['a','b']
df['c'] = np.where(df[cols].eq(0).all(axis=1), 'no','yes')
Here is the simplest solution I can think of:这是我能想到的最简单的解决方案:
import numpy as np
df['c'] = np.where( (df['a']>0) | (df['b']>0), 'yes', 'no')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.