Reputation: 279
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
DESIRE OUTPUT
But, when I use ffill
, I ended up with this which is incorrect. Is there a way I can use ffill
but with conditions?
Upvotes: 0
Views: 457
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
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
Reputation: 26676
Please try
df.groupby('name').email.apply(lambda x: x.fillna(method='ffill'))
Upvotes: 0