按四分之一的平均熊猫枢轴表

发布于 2025-02-07 14:58:31 字数 739 浏览 0 评论 0原文

我有一个我创建的熊猫枢轴桌子,需要按季度日期分组,每季度每个FICO频段的余额加在一起。这些宿舍将是第1季度(2月,3月,3月),第2季度(4月,6月,6月),第3季度(7月,8月,9月,9月,)和第四季度(10月,11月,12月,12月)。因为我试图自动化此过程,所以我也需要在各个宿舍的不同年份进行分组。例如,2015年Q1、2016 Q1等。我用来获取此表的代码是:

total_balance = df.pivot_table('balance','base_ts', 'fico_orig_band')

fico_orig_band  300-649 650-699 700-749 750-799 800-850 invalid missing
base_ts                         
2015-11-28      1000    1000    1000    1000    1000    1000    1000
2015-12-30      2000    2000    2000    2000    2000    2000    2000
2016-01-30      3000    3000    3000    3000    3000    3000    3000
2016-02-27      4000    4000    4000    4000    4000    4000    4000
2016-03-30      5000    5000    5000    5000    5000    5000    5000

I have a pandas pivot table that I created that needs to be grouped by quarterly dates with the balance of each fico band per quarter added together. The quarters would be Q1 (Jan, Feb, March), Q2 (Apr, May, June), Q3 (July, Aug, Sep), and Q4 (Oct, Nov, Dec). Because I'm trying to automate this process I need to group by the quarters for different years, as well. For example, 2015 Q1, 2016 Q1, etc. The code I used to get this table was:

total_balance = df.pivot_table('balance','base_ts', 'fico_orig_band')

fico_orig_band  300-649 650-699 700-749 750-799 800-850 invalid missing
base_ts                         
2015-11-28      1000    1000    1000    1000    1000    1000    1000
2015-12-30      2000    2000    2000    2000    2000    2000    2000
2016-01-30      3000    3000    3000    3000    3000    3000    3000
2016-02-27      4000    4000    4000    4000    4000    4000    4000
2016-03-30      5000    5000    5000    5000    5000    5000    5000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦亦如幻 2025-02-14 14:58:31

您可以将日期转换为四分之一,然后在汇总之前:

out = (df
 .assign(ts_band=pd.to_datetime(df['ts_band']).dt.to_period('Q-MAR'))
 .pivot_table('balance', 'base_ts', 'fico_orig_band')
)

You can convert your dates to quarter before aggregation:

out = (df
 .assign(ts_band=pd.to_datetime(df['ts_band']).dt.to_period('Q-MAR'))
 .pivot_table('balance', 'base_ts', 'fico_orig_band')
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文