[英]How to create conditional pandas series/column?
Here is a sample df:这是一个示例df:
A B C D E (New Column)
0 1 2 a n ?
1 3 3 p d ?
2 5 9 f z ?
If Column A == Column B PICK Column C's value apply to Column E;如果 A 列 == B 列 PICK C 列的值适用于 E 列; Otherwise PICK Column D's value apply to Column E.
否则 PICK D 列的值适用于 E 列。
I have tried many ways but failed, I am new please teach me how to do it, THANK YOU!我尝试了很多方法但都失败了,我是新手,请教我怎么做,谢谢!
Note: It needs to PICK the value from Col.C or Col.D in this case.注意:在这种情况下,它需要从 Col.C 或 Col.D 中选择值。 So there are not specify values are provided to fill in the Col.E(this is the most different to other similar questions)
所以没有提供指定值来填写Col.E(这是与其他类似问题最不同的)
use numpy.where使用numpy.where
df['E'] = np.where(df['A'] == df['B'],
df['C'],
df['D'])
df
A B C D E
0 1 2 a n n
1 3 3 p d p
2 5 9 f z z
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.