[英]Keep values of between two columns based on third column in pandas
I have three columns, A, B and C. I want to create a fourth column D that contains values of A or B, based on the value of C. For example: 我有三列,A,B和C.我想创建第四列D,其中包含A或B的值,基于C的值。例如:
A B C D
0 1 2 1 1
1 2 3 0 3
2 3 4 0 4
3 4 5 1 4
In the above example, column D takes the value of column A if the value of C is 1 and the value of column B if the value of C is 0. Is there an elegant way to do it in Pandas? 在上面的例子中,如果C的值为1,则列D取A列的值,如果C的值为0,则取列B的值。在Pandas中有优雅的方法吗? Thank you for your help.
谢谢您的帮助。
Use numpy.where
: 使用
numpy.where
:
In [20]: df
Out[20]:
A B C
0 1 2 1
1 2 3 0
2 3 4 0
3 4 5 1
In [21]: df['D'] = np.where(df.C, df.A, df.B)
In [22]: df
Out[22]:
A B C D
0 1 2 1 1
1 2 3 0 3
2 3 4 0 4
3 4 5 1 4
pandas
In consideration of the OP's request 考虑到OP的要求
Is there an elegant way to do it in Pandas?
在熊猫中有优雅的方式吗?
my opinion of elegance 我对优雅的看法
and idiomatic pure pandas
和惯用的纯
pandas
assign
+ pd.Series.where
assign
+ pd.Series.where
df.assign(D=df.A.where(df.C, df.B))
A B C D
0 1 2 1 1
1 2 3 0 3
2 3 4 0 4
3 4 5 1 4
response to comment 回应评论
how would you modify the pandas answer if instead of 0, 1 in column C you had A, B?
你如何修改pandas的答案,而不是0,在C列中你有A,B?
df.assign(D=df.lookup(df.index, df.C))
A B C D
0 1 2 A 1
1 2 3 B 3
2 3 4 B 4
3 4 5 A 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.