简体   繁体   English

大熊猫的前一组均值漂移

[英]Previous group mean shift in pandas

I have a table that looks like this:我有一张看起来像这样的表:

date         revenue
2021-01-01   20
2021-01-02   30
...
2021-01-31   50
2021-02-01   35
2021-02-02   67

I want to calculate for each row maximum revenue for the previous month.我想计算上个月每一行的最大收入。

I can calculate maximum revenue for the current month:我可以计算当月的最大收入:

df['max']=df.groupby(df['date'].dt.month)[revenue'].transform(max)

And it will look like this:它看起来像这样:

  date         revenue  max
2021-01-01   20         50
2021-01-02   30         50
...
2021-01-31   50         50
2021-02-01   35         67
2021-02-02   67         67

But I want it to be:但我希望它是:

  date         revenue  max
2021-01-01   20         nan
2021-01-02   30         nan
...
2021-01-31   50         nan
2021-02-01   35         50
2021-02-02   67         50

I tried to put .shift() in the end but it will only lag/lead max by rows, yet I need it by group.我试图把.shift()放在最后,但它只会按行滞后/领先,但我需要按组。

Please help请帮忙

You can do groupby with shift map您可以使用shift map进行groupby

s = df.date.dt.strftime('%Y%m')
df['new'] = s.map(df.groupby(s).revenue.max().shift())
df
Out[62]: 
        date  revenue   new
0 2021-01-01       20   NaN
1 2021-01-02       30   NaN
2 2021-01-31       50   NaN
3 2021-02-01       35  50.0
4 2021-02-02       67  50.0

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

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