简体   繁体   English

Pandas:用另一个 DataFrame 替换 NaN

[英]Pandas: replace a NaN with another DataFrame

I'm trying to figure this out so please help me, I have this dataset:我正在尝试解决这个问题,所以请帮助我,我有这个数据集:

df1= pd.DataFrame(data={'col1': ['a','b','c','d'],
                              'col2': [1,2,np.nan,4]})
df2=pd.DataFrame(data={'col1': ['a','b','b','a','f','c','e','d','e','a'],
                       'col2':[1,3,2,3,6,4,1,2,5,2]})

df1 df1

  col1  col2
0    a   1.0
1    b   2.0
2    c   NaN
3    d   4.0

df2 df2

  col1  col2
0    a     1
1    b     3
2    b     2
3    a     3
4    f     6
5    c     4
6    e     1
7    d     2
8    e     5
9    a     2

I tried this我试过这个

df1[df1['col2'].isna()] = pd.merge(df1, df2, on=['col1'], how='left')

I expected this我期待这个

  col1  col2
0    a   1.0
1    b   2.0
2    c   4
3    d   4.0

but instead, I got this但相反,我得到了这个

  col1  col2
0    a   1.0
1    b   2.0
2    a   NaN
3    d   4.0

I then tried this然后我尝试了这个

for x in zip(df1,df2):
    if x in df1['col2'] == x in df2['col2']:
        df1['col1'][df1['col1'].isna()] = df2['col1'].where(df1['col2'][x] == df2['col2'][x])

but got this但得到了这个

  col1  col2
0    a   1.0
1    b   2.0
2    c   NaN
3    d   4.0

I also tried this answer我也试过这个答案

but still nothing但仍然没有

Use Series.map for match values by col1 with Series with unique column col1 by DataFrame.drop_duplicates and replace only missing values by Series.fillna :使用Series.mapcol1 col1 Series匹配DataFrame.drop_duplicates并仅用Series.fillna替换缺失值:

s = df2.drop_duplicates('col1').set_index('col1')['col2']
print (s)
col1
a    1
b    3
f    6
c    4
e    1
d    2
Name: col2, dtype: int64

print (df1['col1'].map(s))
0    1
1    3
2    4
3    2
Name: col1, dtype: int64

df1['col2'] = df1['col2'].fillna(df1['col1'].map(s))
print (df1)
  col1  col2
0    a   1.0
1    b   2.0
2    c   4.0
3    d   4.0

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

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