如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列

时间:2018-11-20 07:00:18

标签: python pandas dataframe matching

我有两个数据帧,我想将值从df2的“ Result2”列复制到df1的“ Result1”列,并为同一行中的值从df2到df1添加“描述”列如果两个数据框中的“ ID”列中的值都匹配,则为“ Result2”列中的值(已复制)。

以下是数据帧:

df1

ID              Result1
Position1       NaN
MRS1            OK
MRS2            Maybe
MRS3            NOK
Position2       NaN
MRS4            OK
MRS5            OK
Position3       NaN
MRS6            OK
Position4       NaN
MRS7            NOK
MRS8            OK
MRS9            NOK
MRS10           Excellent

df2

ID              Result2              Description
text            NaN                  NaN
NaN             NaN                  text
text            NaN                  NaN
MRS1            OK                   New
MRS2            OK                   Old
NaN             NaN                  NaN
NaN             NaN                  text
MRS3            OK                   New
MRS4            OK                   New
MRS5            OK                   NaN
MRS6            NOK                  Very old
NaN             text                 NaN
text            text                 NaN
NaN             NaN                  NaN
MRS7            Excellent            Very old
MRS8            Excellent            Old
text            text                 text
MRS9            Excellent            New
NaN             NaN                  NaN
MRS10           NOK                  New

我想得到:

ID              Result1             Result2
Position1       NaN                  NaN
MRS1            OK                   New
MRS2            OK                   Old
MRS3            OK                   New
Position2       NaN                  NaN
MRS4            OK                   New
MRS5            OK                   NaN            
Position3       NaN                  NaN
MRS6            NOK                  Very old
Position4       NaN                  NaN
MRS7            Excellent            Very old
MRS8            Excellent            Old
MRS9            Excellent            New
MRS10           NOK                  New

然后我将NaN放在应该是一个空单元格的地方。如何在熊猫中做到这一点? 我尝试了合并,但是我遇到了问题,因为它要求比较列的长度相同。我只想比较两个数据帧的“ ID”列中的值,而这些列的长度并不相同。

1 个答案:

答案 0 :(得分:1)

仅将ID列与左联接和rename列一起使用merge

df = (df1[['ID']].merge(df2, on='ID', how='left')
                 .rename(columns={'Result2':'Result1','Description':'Result2'}))
print (df)
           ID    Result1   Result2
0   Position1        NaN       NaN
1        MRS1         OK       New
2        MRS2         OK       Old
3        MRS3         OK       New
4   Position2        NaN       NaN
5        MRS4         OK       New
6        MRS5         OK       NaN
7   Position3        NaN       NaN
8        MRS6        NOK  Very old
9   Position4        NaN       NaN
10       MRS7  Excellent  Very old
11       MRS8  Excellent       Old
12       MRS9  Excellent       New
13      MRS10        NOK       New