简体   繁体   中英

Pandas Assign Row to matching row's index

Given two columns in pandas how do I assign the row of column A to the index of the matching row in column B, given repeats in column A?

Data:

   A  B 
0  x  k
1  y  z 
2  z  y
3  k  x
4  x  nAn

Output:

   A  B 
0  3  k
1  2  z 
2  1  y
3  0  x
4  3  nAn

I have tried looping through each row and assigning A's row to the index of the matching row in B, however I feel there is a more efficient way to do this.

Assuming values in A will always belong to some value in B, you can use broadcasted comparison for performance:

df['A'] = (df.B.values == df.A[:, None]).argmax(1)
df
   A    B
0  3    k
1  2    z
2  1    y
3  0    x
4  3  nAn

Use map

df.assign(A=df.A.map(dict(zip(df.B, df.index))))

   A    B
0  3    k
1  2    z
2  1    y
3  0    x
4  3  nAn

Using Series.get

pd.Series(df.index,index=df.B).get(df.A)
Out[135]: 
A
x    3
y    2
z    1
k    0
x    3
dtype: int64

#df['A']=pd.Series(df.index,index=df.B).get(df.A).values

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM