user458
user458

Reputation: 399

How to fillna by groups?

I have the following dataframe

id day name
1  10  kate
1  11  paul
1  12  paul 
2  1   Nan
2  2   leonardo
2  3   Nan
3  1   Nan

How can I rewrite this function df['name'].fillna(method='ffill', inplace=True), so that I get this result?

id day name
1  10  kate
1  11  paul
1  12  paul 
2  1   Nan
2  2   leonardo
2  3   leonardo
3  1   Nan

Upvotes: 1

Views: 1018

Answers (2)

tlentali
tlentali

Reputation: 3455

From your DataFrame :

import pandas as pd
from io import StringIO

>>> df = pd.read_csv(StringIO("""
id,day,name
1,10,kate
1,11,paul
1,12,paul 
2,1,
2,2,leonardo
2,3,
3,1,"""), sep=',')
>>> df
    id  day name
0   1   10  kate
1   1   11  paul
2   1   12  paul
3   2   1   NaN
4   2   2   leonardo
5   2   3   NaN
6   3   1   NaN

We can use a groupby and the ffill() method to get the expected result :

>>> df['name'] = df.groupby(['id'])['name'].ffill()
>>> df
    id  day name
0   1   10  kate
1   1   11  paul
2   1   12  paul
3   2   1   NaN
4   2   2   leonardo
5   2   3   leonardo
6   3   1   NaN

Upvotes: 2

alparslan mimaroğlu
alparslan mimaroğlu

Reputation: 1490

You can forward fill with a groupby like this:

df = df.groupby(['id'], as_index=False).apply(lambda group: group.ffill())

This will also fill other na columns if you have them. You can just use name column if you wanted to

Upvotes: 0

Related Questions