denpy
denpy

Reputation: 279

Forward Fill NA

I have a table with NaN.

import pandas as pd

data = {'name': ['may','may', 'mary', 'james','james','john','paul', 'paul', 'joseph'],
       'email' : ['[email protected]','NaN','[email protected]','[email protected]','NaN','NaN','[email protected]','NaN','NaN']}

df = pd.DataFrame(data)

BEFORE

enter image description here

DESIRE OUTPUT

enter image description here

But, when I use ffill, I ended up with this which is incorrect. Is there a way I can use ffill but with conditions?

enter image description here

Upvotes: 0

Views: 457

Answers (3)

Agaz Wani
Agaz Wani

Reputation: 5684

Another way could be

import pandas as pd
import numpy as np
df = df.replace("NaN", np.nan)
df.update(df.groupby('name')['email'].ffill().fillna("NaN"))
df

    name    email
0   may     [email protected]
1   may     [email protected]
2   mary    [email protected]
3   james   [email protected]
4   james   [email protected]
5   john    NaN
6   paul    [email protected]
7   paul    [email protected]
8   joseph  NaN

Upvotes: 0

jtorca
jtorca

Reputation: 1551

In your example, NaN values are strings, with value "NaN". So before you fillna, you'd have to convert those to actual null values.

import pandas as pd
import numpy as np

data = {'name': ['may','may', 'mary', 'james','james','john','paul', 'paul', 'joseph'],
       'email' : ['[email protected]','NaN','[email protected]','[email protected]','NaN','NaN','[email protected]','NaN','NaN']}

df = pd.DataFrame(data)

df['email'] = df['email'].replace({'NaN':np.nan})
df['email'] = df.groupby('name')['email'].fillna(method='ffill')
df
     name            email
0     may    [email protected]
1     may    [email protected]
2    mary   [email protected]
3   james  [email protected]
4   james  [email protected]
5    john              NaN
6    paul   [email protected]
7    paul   [email protected]
8  joseph              NaN

Upvotes: 1

wwnde
wwnde

Reputation: 26676

Please try

df.groupby('name').email.apply(lambda x: x.fillna(method='ffill'))

Upvotes: 0

Related Questions