根据另一个列值更改 Pandas DataFrame 列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13061478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change a pandas DataFrame column value based on another column value
提问by John
I have a dataframe with two columns each of which represents an organism. They are called ORG1 and ORG2 I want to move the values of ORG2 into ORG1 for the corresponding index value.
我有一个包含两列的数据框,每列代表一个生物体。它们被称为 ORG1 和 ORG2 我想将 ORG2 的值移动到 ORG1 中以获得相应的索引值。
So, if ORG1 is 'A' and ORG2 is 'B' I want ORG1 to take the value 'B' from ORG2.
因此,如果 ORG1 是“A”而 ORG2 是“B”,我希望 ORG1 从 ORG2 中获取值“B”。
I have already started work to identify indexes of the ORG2 organisms that I want to move, as follows:
我已经开始工作来确定我想要移动的 ORG2 生物的索引,如下所示:
def move_org2(x):
org2_matches = Series(x.ORG2.str.count("ESBL"))
return x.ix[org2_matches == 1]
org2_DF = move_org2(DF)
org2_DF.ORG2.index
What is the best way to use this to change ORG1 values with the values at corresponding ORG2 indices
使用此方法将 ORG1 值更改为相应 ORG2 索引处的值的最佳方法是什么
回答by Wouter Overmeire
In [13]: df
Out[13]:
ORG1 ORG2
0 A ESBL
1 B P
2 C Q
3 D R
4 E ESBL
In [14]: cond = df.ORG2 == 'ESBL'
In [15]: df.ORG1[cond] = df.ORG2[cond]
In [16]: df
Out[16]:
ORG1 ORG2
0 ESBL ESBL
1 B P
2 C Q
3 D R
4 ESBL ESBL
回答by Zero
In other words, using .locyou would do
换句话说,使用.loc你会做
In [2008]: df
Out[2008]:
ORG1 ORG2
0 A ESBL
1 B P
2 C Q
3 D R
4 E ESBL
In [2009]: df.loc[df['ORG2'] == 'ESBL', 'ORG1'] = df['ORG2']
In [2010]: df
Out[2010]:
ORG1 ORG2
0 ESBL ESBL
1 B P
2 C Q
3 D R
4 ESBL ESBL
Or, if you need a copy, without modifying original df, you can use .mask()
或者,如果您需要副本,而无需修改 original df,则可以使用.mask()
In [2016]: df.mask(df['ORG2'] == 'ESBL', df['ORG2'], axis=0)
Out[2016]:
ORG1 ORG2
0 ESBL ESBL
1 B P
2 C Q
3 D R
4 ESBL ESBL

