Muzaffer
Muzaffer

Reputation: 31

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?

Upvotes: 3

Views: 2021

Answers (2)

jpp
jpp

Reputation: 164673

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

Upvotes: 4

BENY
BENY

Reputation: 323226

You need groupby with ffill and bfill

themes.groupby('code').apply(lambda x : x.ffill().bfill())

Upvotes: 4

Related Questions