0

I have 2 dataframes with different number of row. So, lets say:

df1 = {'Name': ['Tom', 'Joseph', 'Krish', 'John','Micheal'], 'Age': [20, 21, 19, 18, 23], 'HeightEach': [156, 167,149 , 151, 149]}  
df2 = {'Name': ['Tom', 'Joseph', 'Krish', 'John'], 'Age': [20, 21, 19, 18], 'Height': [179, NaN,159 , NaN]}  

The issue now is , I want to replace the null values in 'Height' column with the same value in df1. So far I only know how to replace NaN with 0 like below code:

 df1['Height'] = df1['Height'].replace(np.nan, 0)

However, i do not know how to replace the NaN with this condition:

  • Replace NaN with same height in df1 if the name in df1 and df2 is same. I tried to do like simple code below well at least to get the data with the same name:

    df1['Height'] = df1["Name"] == df2["Name"]

However it returns this error:

enter image description here

Edit: dataset for both data is not same. So, I just want to save the NaN data and ignore the existing data in df2.

So, i've tried:

if df1["Name"] == df2["Name"]:
    result = df2.merge(df1, how='inner')
    result.loc[result['Height'].isna(), 'Height'] = result['HeightEach']

It throws error:

enter image description here

2
  • use fillna('value') Commented Jun 17, 2021 at 9:53
  • df1['Height'][df1['Height'] == np.nan] = df2['Height']. Might also need to df1.set_index('Name') Commented Jun 17, 2021 at 9:55

1 Answer 1

1

Left merge df2, and df1, then fill NaN values by HeightEach

result = df2.merge(df1, how='left')
result.loc[result['Height'].isna(), 'Height'] = result['HeightEach']

     Name  Age  Height  HeightEach
0     Tom   20   156.0         156
1  Joseph   21   167.0         167
2   Krish   19   149.0         149
3    John   18   151.0         151

It may not work in cases when there isn't value in df1 for all values in df2, you may want to do inner merge first and fill the NaN values in that scenario.

result = df2.merge(df1, how='inner')
result.loc[result['Height'].isna(), 'Height'] = result['HeightEach']

df2.append(result)[['Name', 'Age', 'Height']].drop_duplicates(keep='last').dropna()
     Name  Age  Height
0     Tom   20   156.0
1  Joseph   21   167.0
2   Krish   19   149.0
3    John   18   151.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.