df:
id name
0 toto
1 tata
0 NaN
I would like to impute the name column missing value on the third row based on the id.
The desired dataframe would be:
id name
0 toto
1 tata
0 toto
I did the following:
df.loc[df.name.isna(), "name"] = df["id"].map(df["name"])
but it is not working.
>Solution :
import pandas as pd
df = pd.DataFrame({'id':[0,1,0],
'name':['toto','tata',pd.NA]})
df = df[['id']].merge(df[pd.notna(df['name'])],
how = 'left',
on = 'id')
df