按四分之一的平均熊猫枢轴表
我有一个我创建的熊猫枢轴桌子,需要按季度日期分组,每季度每个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将日期转换为四分之一,然后在汇总之前:
You can convert your dates to quarter before aggregation: