简体   繁体   English

根据不同的DataFrame在Pandas DataFrame中添加一列

[英]Appending a column in a pandas DataFrame based on a different DataFrame

I have these two DataFrames: 我有以下两个DataFrame:

df1

       A   B
    0  8   x
    1  3   y
    2  1   x
    3  2   z
    4  7   y

    and

df2    
       A   B
    0  x   hey
    1  y   there
    2  z   sir

I am trying to append df1['B'] based on df2['B'] so that my desired output would be: 我试图基于df2['B']附加df1['B'] ,以便我想要的输出为:

df3

       A   B
    0  8   hey
    1  3   there
    2  1   hey
    3  2   sir
    4  7   there

Is there a built in method for doing this kind of transformation? 有内置的方法可以进行这种转换吗? I was experimenting with something similar to this, but I could not get it to function properly: 我正在尝试与此类似的东西,但无法使其正常运行:

for x in df1['B']:
    if df2['A'] == x:
        df1['B'].append.df2['B']

I believe you'll want a simple merge: 我相信您会想要一个简单的合并:

In [10]: df1.merge(df2, left_on='B', right_on='A')
Out[10]:
   A_x B_x A_y    B_y
0    8   x   x    hey
1    1   x   x    hey
2    3   y   y  there
3    7   y   y  there
4    2   z   z    sir

And if you'd like to keep the order of the index: 如果您想保持索引的顺序:

In [11]: df1.reset_index().merge(df2, left_on='B', right_on='A').set_index('index').sort_index()
Out[11]:
       A_x B_x A_y    B_y
index
0        8   x   x    hey
1        3   y   y  there
2        1   x   x    hey
3        2   z   z    sir
4        7   y   y  there

you can set column A as an index to look-up values in column B : 您可以将A列设置为索引,以查找B列中的值:

>>> df1.B = df2.set_index('A').loc[df1.B,'B'].values
>>> df1
   A      B
0  8    hey
1  3  there
2  1    hey
3  2    sir
4  7  there

[5 rows x 2 columns]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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