[英]Transferring values from multiple columns in a dataframe to a new column in another dataframe, based on time-criterion
[英]New column based on another multiple dataframe columns
下面是我的数据框。
一个 | b |
---|---|
12 | 0 |
1 | 21 |
0 | 0 |
现在要添加一个列名“c”,当 a 或 b 为非零时返回 yes,当 a 和 b 为零时返回 no
一个 | b | C |
---|---|---|
12 | 0 | 是的 |
1 | 21 | 是的 |
0 | 0 | 不 |
如果需要测试所有列比较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
另一个想法是通过DataFrame.any
测试是否至少一个值不为0
,然后使用映射:
df['c'] = df.ne(0).any(axis=1).map({False: 'no',True:'yes'})
如果可能的话,多列并且只需要测试a,b
列:
cols = ['a','b']
df['c'] = np.where(df[cols].eq(0).all(axis=1), 'no','yes')
这是我能想到的最简单的解决方案:
import numpy as np
df['c'] = np.where( (df['a']>0) | (df['b']>0), 'yes', 'no')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.