如何从另一个 df 填充 Pandas df 中列的值?

时间:2021-06-30 04:26:15

标签: python pandas dataframe numpy

我有一个数据框:

dict1 = {'Name': ['abc', 'def', 'ghi' , 'jkl'], 'Group': ['Group1', 'Group2', np.nan, np.nan], 'Class' : ['ClassA', 'ClassB' , np.nan, np.nan]}
df1 = pd.DataFrame(dict1)
df1:
    Class   Group Name
0  ClassA  Group1  abc
1  ClassB  Group2  def
2     NaN     NaN  ghi
3     NaN     NaN  jkl

这里我想通过从另一个 df 中查找名称及其相应的组和类值来替换 NaN 值:

dict2 = {'Name': ['ghi', 'jkl'], 'Group': ['Group3', 'Group4'], 'Class':['ClassC', 'ClassD']}
df2 = pd.DataFrame(dict2)
df2:
    Class   Group Name
0  ClassC  Group3  ghi
1  ClassD  Group4  jkl

所以基本上我想通过查看 df2 中的映射来替换 df1 值。所以我的 df1 应该是这样的:

df1:
    Class   Group Name
0  ClassA  Group1  abc
1  ClassB  Group2  def
2  ClassC  Group3  ghi
3  ClassD  Group4  jkl

我尝试过 df.loc,但我不知道如何让它在这种情况下工作。

2 个答案:

答案 0 :(得分:0)

通过 append()dropna() 尝试:

out=df1.append(df2).dropna(subset=['Group','Class'])
#you can also use pd.concat() in place of append() method

通过 merge()fillna()drop()

out=df1.merge(df2,on='Name',how='left',suffixes=('','_y'))
out=out.fillna({'Group':out['Group_y'],'Class':out['Class_y']})
out=out.drop(out.filter(like='_').columns,1)

out 的输出:

    Name    Group   Class
0   abc     Group1  ClassA
1   def     Group2  ClassB
2   ghi     Group3  ClassC
3   jkl     Group4  ClassD

答案 1 :(得分:0)

您可以尝试这样做:

dfNew=df1.merge(df2,on='Name',how='outer',suffixes=('','_y'))
dfNew['Group']=dfNew['Group'].fillna(dfNew['Group_y'])
dfNew['Class']=dfNew['Class'].fillna(dfNew['Class_y'])
dfNew=dfNew.drop(dfNew.filter(like='_').columns,1)

Out[35]: 
  Name   Group   Class
0  abc  Group1  ClassA
1  def  Group2  ClassB
2  ghi  Group3  ClassC
3  jkl  Group4  ClassD