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
heightin 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:
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:


fillna('value')df1['Height'][df1['Height'] == np.nan] = df2['Height']. Might also need todf1.set_index('Name')