问题描述:

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
相关阅读:
Top