如何计算大 pandas 的逆积 [英] How to calculate inverse cumsum in pandas

查看:73
本文介绍了如何计算大 pandas 的逆积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找到一种方法来计算大熊猫的反向积.这意味着从下到上应用 cumsum .我面临的问题是,我正在尝试查找西班牙上下每个月的可工作日数(第一可工作日= 1,第二可工作日= 2,第三可工作日= 3,等等...)和从下到上(最后一个可行的日期= 1,最后一个工作日= 2,依此类推).到目前为止,我设法使自上而下的顺序起作用,但无法获得逆序的顺序,我进行了很多搜索,无法找到一种执行逆累积和的方法:

I am trying to find a way to calculate an inverse cumsum for pandas. This means applying cumsum but from bottom to top. The problem I'm facing is, I'm trying to find the number of workable day for each month for Spain both from top to bottom (1st workable day = 1, 2nd = 2, 3rd = 3, etc...) and bottom to top (last workable day = 1, day before last = 2, etc...). So far I managed to get the top to bottom order to work but can't get the inverse order to work, I've searched a lot and couldn't find a way to perform an inverse cummulative sum:

import pandas as pd
from datetime import date
from workalendar.europe import Spain
import numpy as np
cal = Spain()
#print(cal.holidays(2019))
rng = pd.date_range('2019-01-01', periods=365, freq='D')
df = pd.DataFrame({ 'Date': rng})
df['flag_workable'] = df['Date'].apply(lambda x: cal.is_working_day(x))
df_workable = df[df['flag_workable'] == True]
df_workable['month'] = df_workable['Date'].dt.month
df_workable['workable_day'] = df_workable.groupby('month')['flag_workable'].cumsum()
print(df)
print(df_workable.head(30))

一月份的输出:

         Date  flag_workable  month  workable_day
1  2019-01-02           True      1           1.0
2  2019-01-03           True      1           2.0
3  2019-01-04           True      1           3.0
6  2019-01-07           True      1           4.0
7  2019-01-08           True      1           5.0

一月最后几天的示例:

         Date  flag_workable  month  workable_day
24 2019-01-25           True      1          18.0
27 2019-01-28           True      1          19.0
28 2019-01-29           True      1          20.0
29 2019-01-30           True      1          21.0
30 2019-01-31           True      1          22.0

这是应用反累积量后的预期输出:

This would be the expected output after applying the inverse cummulative:

         Date  flag_workable  month  workable_day  inv_workable_day
1  2019-01-02           True      1           1.0              22.0
2  2019-01-03           True      1           2.0              21.0
3  2019-01-04           True      1           3.0              20.0
6  2019-01-07           True      1           4.0              19.0
7  2019-01-08           True      1           5.0              18.0

一月的最后几天:

         Date  flag_workable  month  workable_day  inv_workable_day
24 2019-01-25           True      1          18.0               5.0
27 2019-01-28           True      1          19.0               4.0
28 2019-01-29           True      1          20.0               3.0
29 2019-01-30           True      1          21.0               2.0
30 2019-01-31           True      1          22.0               1.0

推荐答案

将DataFrame 之前的行顺序转换为分组,以便以相反的顺序计算 cumsum 每个月之内.

Invert the row order of the DataFrame prior to grouping so that the cumsum is calculated in reverse order within each month.

df['inv_workable_day'] = df[::-1].groupby('month')['flag_workable'].cumsum()
df['workable_day'] = df.groupby('month')['flag_workable'].cumsum()

#         Date  flag_workable  month  inv_workable_day  workable_day
#1  2019-01-02           True      1               5.0           1.0
#2  2019-01-03           True      1               4.0           2.0
#3  2019-01-04           True      1               3.0           3.0
#6  2019-01-07           True      1               2.0           4.0
#7  2019-01-08           True      1               1.0           5.0
#8  2019-02-01           True      2               1.0           1.0

这篇关于如何计算大 pandas 的逆积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆