Gokkul
Gokkul

Reputation: 57

How to fill in between rows gap comparing with other dataframe using pandas?

I want to compare df1 with df2 and fill only the blanks without overwriting other values. I have no idea how to achieve this without overwriting or creating an extra columns.

Can I do this by converting df2 into dictionary and mapping with df1?

df1 = pd.DataFrame({'players name':['ram', 'john', 'ismael', 'sam', 'karan'],
                   'hobbies':['jog','','photos','','studying'],
                   'sports':['cricket', 'basketball', 'chess', 'kabadi', 'volleyball']})

df1:

  players name   hobbies      sports
0          ram       jog     cricket
1         john            basketball
2       ismael    photos       chess
3          sam                kabadi
4        karan  studying  volleyball

And, df,

df2 = pd.DataFrame({'players name':['jagan', 'mohan', 'john', 'sam', 'karan'],
                   'hobbies':['riding', 'tv', 'sliding', 'jumping', 'studying']})

df2:

  players name   hobbies
0        jagan    riding
1        mohan        tv
2         john   sliding
3          sam   jumping
4        karan  studying

I want output like this:

enter image description here

Upvotes: 2

Views: 157

Answers (2)

Akhilesh_IN
Akhilesh_IN

Reputation: 1317

if blank space is NaN value

df1 = pd.DataFrame({"players name":["ram","john","ismael","sam","karan"],
"hobbies":["jog",pd.np.NaN,"photos",pd.np.NaN,"studying"],
"sports":["cricket","basketball","chess","kabadi","volleyball"]})

then

dicts = df2.set_index("players name")['hobbies'].to_dict()
df1['hobbies'] = df1['hobbies'].fillna(df1['players name'].map(dicts))

output:

  players name   hobbies      sports
0          ram       jog     cricket
1         john   sliding  basketball
2       ismael     photos       chess
3          sam   jumping      kabadi
4        karan  studying  volleyball

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153460

Try this:

df1['hobbies'] = (df1['players name'].map(df2.set_index('players name')['hobbies'])
                                     .fillna(df1['hobbies']))
df1

Output:

  players name   hobbies      sports
0          ram       jog     cricket
1         john   sliding  basketball
2       ismael    photos       chess
3          sam   jumping      kabadi
4        karan  studying  volleyball

Upvotes: 2

Related Questions