小编Ben*_*Ben的帖子

在pandas中有效地聚合重新采样的日期时间集合

给出以下数据集作为pandas dataframe df:

index(as DateTime object) |  Name        |  Amount    |  IncomeOutcome
---------------------------------------------------------------
2019-01-28                |  Customer1   |  200.0     |  Income
2019-01-31                |  Customer1   |  200.0     |  Income
2019-01-31                |  Customer2   |  100.0     |  Income
2019-01-28                |  Customer2   |  -100.0    |  Outcome
2019-01-31                |  Customer2   |  -100.0    |  Outcome
Run Code Online (Sandbox Code Playgroud)

我们执行以下步骤:

grouped = df.groupby("Name", "IncomeOutcome")
sampled_by_month = grouped.resample("M")
aggregated = sampled_by_month.agg({"MonthlyCount": "size", "Amount": "sum"})
Run Code Online (Sandbox Code Playgroud)

所需的输出应如下所示:

Name       |  IncomeOutcome   |  Amount    |  MonthlyCount
------------------------------------------------------------
Customer1  |  Income          |  400.0     |  2
Customer2  | …
Run Code Online (Sandbox Code Playgroud)

python performance numpy pandas

7
推荐指数
1
解决办法
240
查看次数

标签 统计

numpy ×1

pandas ×1

performance ×1

python ×1