如何计算Pandas的逆积和

2024-04-30 00:26:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图找到一种方法来计算熊猫的逆总和。这意味着应用cumsum,但从下到上。我所面临的问题是,我试图从上到下(第一个工作日=1,第二个工作日=2,第三个工作日=3,等等)和从下到上(最后一个工作日=1,前一个工作日=2,等等)找出西班牙每个月的工作日数。 到目前为止,我成功地实现了从上到下的顺序,但无法实现相反的顺序,我已经搜索了很多,但找不到执行逆累加和的方法:

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

这将是应用逆累积公式后的预期输出:

         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

Tags: 方法importtruedfdate顺序calflag
2条回答

将DataFrameprior的行顺序反转为grouping,以便在每个月内以相反顺序计算cumsum。你知道吗

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

解决方案

无论您想对哪个列应用cumsum,都有两个选项:
1将该列的副本按索引降序,然后按总和,然后按索引升序。最后将其赋回到数据帧列。你知道吗

  1. 使用numpy:
import numpy as np

array = df.column_data.to_numpy()    
array = np.flip(array)  # to flip the order 
array = numpy.cumsum(array)    
array =  numpy.flip(array)  # to flip back to original order    
df.column_data_cumsum = array   

相关问题 更多 >