简体   繁体   English

在 pandas 中满足条件后按组前向填充缺失值

[英]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:我的 dataframe 看起来像这样:

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:所以,我需要做的是,在 dummy 获得 value = 1 之后,我需要为每个id填充 amount 变量为零,如下所示:

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.我猜我需要groupby('id')fillna(method='ffill') ,也许是.locshift()的某种组合,但我尝试的一切都有一些问题或非常慢。 Any suggestions?有什么建议么?

The way I will use我将使用的方式

s = df.groupby('id')['dummy'].ffill().eq(1)
df.loc[s&df.dummy.isna(),'amount']=0

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.这将 select 所有 dummy 为 nan 的行并用 0 填充金额列。

IIUC, ffill() and mask the still-nan: IIUC、 ffill()和掩码 Still-nan:

s = df.groupby('id')['amount'].ffill().notnull()
df.loc[df['amount'].isna() & s, 'amount'] = 0

Output: Output:

   id  amount  dummy
0   1   130.0    0.0
1   1   120.0    0.0
2   1   110.0    1.0
3   1     0.0    NaN
4   1     0.0    NaN
5   2     NaN    0.0
6   2    50.0    0.0
7   2    20.0    1.0
8   2     0.0    NaN
9   2     0.0    NaN

Could you please try following.请您尝试以下操作。

df.loc[df['dummy'].isnull(),'amount']=0
df

Output will be as follows. Output 如下。

    id  amount  dummy
0   1   130.0   0.0
1   1   120.0   0.0
2   1   110.0   1.0
3   1   0.0     NaN
4   1   0.0     NaN
5   2   NaN     0.0
6   2   50.0    0.0
7   2   20.0    1.0
8   2   0.0     NaN
9   2   0.0     NaN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM