根据另一个列值更改大 pandas DataFrame列值 [英] Change a pandas DataFrame column value based on another column value

查看:113
本文介绍了根据另一个列值更改大 pandas DataFrame列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两列的数据框,每个列表示一个有机体。它们被称为ORG1和ORG2
我想将ORG2的值移动到ORG1中作为相应的索引值。



因此,如果ORG1是A和ORG2是'B'我想要ORG1从ORG2中取值'B'。



我已经开始工作,以确定要移动的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

使用这种方式更改ORG1值的最佳方法是使用相应ORG2索引值

解决方案

 在[13]中:df 
输出[13]:
ORG1 ORG2
0 A ESBL
1 BP
2 CQ
3 DR
4 E ESBL

在[14]中:cond = df.ORG2 = ='ESBL'

在[15]中:df.ORG1 [cond] = df.ORG2 [cond]

在[16] :df
Out [16]:
ORG1 ORG2
0 ESBL ESBL
1 BP
2 CQ
3 DR
4 ESBL ESBL


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.

So, if ORG1 is 'A' and ORG2 is 'B' I want ORG1 to take the value 'B' from ORG2.

I have already started work to identify indexes of the ORG2 organisms that I want to move, as follows:

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

解决方案

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

这篇关于根据另一个列值更改大 pandas DataFrame列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆