[英]How to “flatten” a Panda Panel by summing up specific columns
I am still familairizing myself with Pandas, and Python in general, so please excuse if this is a simple question. 我仍然使用Pandas和Python来熟悉自己,所以请原谅这是一个简单的问题。 I'd also like to avoid one liners so I can understand the underlying actions if possible!
我也想避免使用一个衬垫,以便在可能的情况下理解底层动作! :)
:)
I've managed to pull data data which results in a Panel, with four items. 我已经成功地提取了数据数据,这导致了一个Panel,有四个项目。 The key of each item is a calendar quarter:
每个项目的关键是日历季度:
Item '2015-03-31':
Type Quarterly Sales Ending Inventory
Shoes 123,456 50,000
Purses 33,222 10,000
Item '2015-06-30':
Type Quarterly Sales Ending Inventory
Shoes 12,744 56,000
Purses 15,123 9,000
Item '2015-9-30':
Type Quarterly Sales Ending Inventory
Shoes 15,998 35,000
Purses 11,222 15,000
Item '2015-12-31':
Type Quarterly Sales Ending Inventory
Shoes 12,000 45,000
Purses 9,551 7,000
Ultimately, I would like to "flatten" this by summing up the Quarterly Sales , but taking the Type and Ending Inventory from the most recent entry, and have this in a DataFrame. 最后,我想通过总结季度销售来“扁平化”这一点,但是从最近的条目中获取类型和结束库存 ,并将其放在DataFrame中。 So my ending DataFrame would be something like this:
所以我的结尾DataFrame将是这样的:
Type Quarterly Sales Ending Inventory
Shoes 164,198 45,000
Purses 69,118 7,000
I tried using a function such as grouby (eg mypanel.groupby('Type').sum()
), but that ended up summing both Quarterly Sales and Ending Inventory , whereas I want to take the "most recent" Ending Inventory instead. 我尝试使用像grouby这样的函数(例如
mypanel.groupby('Type').sum()
),但最终总结了季度销售 和 结束库存 ,而我想采用“最近的” 结束库存 。 An easy "fix" for this would be to take the resulting DataFrame, and then subtract out the summation of the first three quarters for the Ending Inventory column, but that seems incredibly awkward. 一个简单的“修复”就是获取生成的DataFrame,然后减去Ending Inventory列的前三个季度的总和,但这看起来非常尴尬。
Any suggestions? 有什么建议么?
Thanks! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.