简体   繁体   中英

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

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

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 :

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

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