Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward fill missing values by group after condition is met in pandas

I'm having a bit of trouble with this. My dataframe looks like this:

id    amount    dummy
1      130        0
1      120        0
1      110        1
1      nan       nan 
1      nan       nan   
2      nan        0
2      50         0
2      20         1
2      nan       nan 
2      nan       nan  

So, what I need to do is, after the dummy gets value = 1, I need to fill the amount variable with zeroes for each id, like this:

id    amount    dummy
1      130        0
1      120        0
1      110        1
1       0        nan 
1       0        nan   
2      nan        0
2      50         0
2      20         1
2       0        nan 
2       0        nan 

I'm guessing I'll need some combination of groupby('id'), fillna(method='ffill'), maybe a .loc or a shift() , but everything I tried has had some problem or is very slow. Any suggestions?

like image 997
Juan C Avatar asked Oct 04 '19 13:10

Juan C


2 Answers

The way I will use

s = df.groupby('id')['dummy'].ffill().eq(1)
df.loc[s&df.dummy.isna(),'amount']=0
like image 192
BENY Avatar answered Oct 20 '22 11:10

BENY


You can do this much easier:

data[data['dummy'].isna()]['amount'] = 0

This will select all the rows where dummy is nan and fill the amount column with 0.

like image 27
Zephyrus Avatar answered Oct 20 '22 10:10

Zephyrus