I have a Pandas DataFrame with 2 columns.

I replaced ' ' with NaN's to process faster with fillna, etc..:
themes = themes.apply(lambda x: x.str.strip()).replace('', np.nan)
How can I replace NaN's with matching values from other rows?
You need groupby with ffill and bfill
themes.groupby('code').apply(lambda x : x.ffill().bfill())
One way is to create a series after dropping null values.
Then use pd.Series.fillna with pd.Series.map:
df = pd.DataFrame({'code': [1, 2, 3, 1, 2, 4],
'name': ['A', np.nan, 'C', np.nan, 'B', 'D']})
s = df.set_index('code')['name'].dropna()
df['name'] = df['name'].fillna(df['code'].map(s))
print(df)
code name
0 1 A
1 2 B
2 3 C
3 1 A
4 2 B
5 4 D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With