简体   繁体   English

根据pandas中的第三列保留两列之间的值

[英]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.

相关问题 当两列重复时删除,但根据第三列的值保留(熊猫) - Remove when 2 columns are duplicated, but keep based on value of a third column (pandas) 如果两列的值在第三列 pandas 中相同,则合并两列 - Merge two columns if their values are the same in a third column pandas 大熊猫:按两列分组,然后按第三列的值排序 - pandas: Grouping by two columns and then sorting it by the values of a third column 根据第三列中的条件获取两个不同列的值 - Get values of two different columns based on a condition in a third column 使用熊猫基于其他两列中的值替换列中的值 - Replace values in column based on values in two other columns using pandas 比较两个熊猫数据框列的元素,并基于第三列创建一个新列 - Compare elements of two pandas data frame columns and create a new column based on a third column Python / Pandas - 如何按两列分组,并计算两行之间第三列的值 - Python/Pandas - How to group by two columns and count rows with value from third column between two numbers 添加两列的两个值并将结果分配给熊猫多索引数据帧中的第三列 - Adding two values of two columns and assigning the result to a third column in a pandas multi-index DataFrame 有没有办法在使用 pandas 按第三列中的值分组时将两列中的值相乘? - Is there a way to multiply the values in two columns while grouping by values in third column using pandas? 根据Pandas中其他两个列的相等性从列中提取值 - Extract values from a column based on the equality of two other columns in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM