python - pandas:有条件地聚合连续行
问题描述
我有一个带有连续索引(每个日历日的日期)的数据框和一个不包含每个日期(仅工作日)的参考向量。
我想将数据框重新索引为仅参考向量中的日期,并将缺失的数据聚合到缺失日期部分之前的最新条目(即周末数据应聚合到上周五)。
目前我已经通过循环反向索引并收集周末数据来实现这一点,然后在循环中稍后添加它。我在问是否有更有效的“阵列方式”来做到这一点。
import pandas as pd
import numpy as np
df = pd.DataFrame({'x': np.arange(10), 'y': np.arange(10)**2},
index=pd.date_range(start="2018-01-01", periods=10))
print(df)
ref_dates = pd.date_range(start="2018-01-01", periods=10)
ref_dates = ref_dates[:5].append(ref_dates[7:]) # omit 2018-01-06 and -07
# inefficient approach by reverse-traversing the dates, collecting the data
# and aggregating it together with the first date that's in ref_dates
df.sort_index(ascending=False, inplace=True)
collector = []
for dt in df.index:
if collector and dt in ref_dates:
# data from previous iteration was collected -> aggregate it and reset collector
# first append also the current data
collector.append(df.loc[dt, :].values)
collector = np.array(collector)
# applying aggregation function, here sum as example
aggregates = np.sum(collector, axis=0)
# setting the new data
df.loc[dt,:] = aggregates
# reset collector
collector = []
if dt not in ref_dates:
collector.append(df.loc[dt, :].values)
df = df.reindex(ref_dates)
print(df)
给出输出(第一个:源数据帧,第二个:目标数据帧)
x y
2018-01-01 0 0
2018-01-02 1 1
2018-01-03 2 4
2018-01-04 3 9
2018-01-05 4 16
2018-01-06 5 25
2018-01-07 6 36
2018-01-08 7 49
2018-01-09 8 64
2018-01-10 9 81
x y
2018-01-01 0 0
2018-01-02 1 1
2018-01-03 2 4
2018-01-04 3 9
2018-01-05 15 77 # contains the sum of Jan 5th, 6th and 7th
2018-01-08 7 49
2018-01-09 8 64
2018-01-10 9 81
解决方案
仍然有一个列表理解循环,但有效。
import pandas as pd
import numpy as np
# Create dataframe which contains all days
df = pd.DataFrame({'x': np.arange(10), 'y': np.arange(10)**2},
index=pd.date_range(start="2018-01-01", periods=10))
# create second dataframe which only contains week-days or whatever dates you need.
ref_dates = [x for x in df.index if x.weekday() < 5]
# Set the index of df to a forward filled version of the ref days
df.index = pd.Series([x if x in ref_dates else float('nan') for x in df.index]).fillna(method='ffill')
# Group by unique dates and sum
df = df.groupby(level=0).sum()
print(df)
推荐阅读
- f# - 使用 Moq 如何访问返回的显式类型的属性
- common-lisp - 使用 ~/quicklisp/local-projects 中的包,它使用 asdf:require-system 而不是 ql:quickload 加载
- r - 每次向量中的项目与某个类别相关时,我可以使用 R 获取类别计数吗?
- android - 在 Android 手机上运行应用程序时出现此错误
- react-native - 当变换缩放反应本机时,动画视图是锯齿状的
- vba - 当某个日期大于或等于另一个日期时,如何创建弹出消息?
- rust - Rust 函数签名导致:错误:预期的 `::`, found `,`
- asp.net-core - 如何使用 Windows 身份验证静默登录?
- python - 生成具有重复模式的序列的代码
- c++ - 二维位集和方括号运算符