Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace missing value from another row in pandas

I have a Pandas DataFrame with 2 columns.

enter image description here

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?

like image 218
Muzaffer Avatar asked Apr 09 '26 02:04

Muzaffer


2 Answers

You need groupby with ffill and bfill

themes.groupby('code').apply(lambda x : x.ffill().bfill())
like image 66
BENY Avatar answered Apr 11 '26 02:04

BENY


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
like image 45
jpp Avatar answered Apr 11 '26 01:04

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!