I have a Pandas DataFrame of transactions:
transactions.head():
Amount Date of Transaction Description \
0 39.95 2017-03-30 Fake_Transaction_One
1 2.39 2017-04-01 Fake_Transaction_Two
2 8.03 2017-04-01 Fake_Transaction_Three
3 34.31 2017-04-01 Fake_Transaction_Four
4 10.56 2017-04-03 Fake_Transaction_Five
Purchase_Type year_month
0 Miscellaneous 2017-03
1 tool_expense 2017-04
2 food_and_domestic 2017-04
3 food_and_domestic 2017-04
4 food_and_domestic 2017-04
我在这个DataFrame上运行了一个groupby命令:
grouped_transactions = transactions.groupby(['Purchase_Type','year_month'])['Amount'].sum()
这会生成一个分组对象:
Purchase_Type year_month
tool_expense 2017-04 72.49
Calendar_Event 2017-08 3.94
2017-12 23.92
2018-02 42.91
2018-03 10.91
我希望对此进行groupby命令,例如
grouped_transactions.groups.keys()
然而,我无法如此操作,因为该对象不是一个分组对象,而是一个系列对象:
In: type(grouped_transactions)
Out: pandas.core.series.Series
看起来,grouped_transactions 是一个 groupby 对象,而不是 Series。此外,它是通过在 Pandas DataFrame 上运行 .groupby 方法创建的。因此,我不确定为什么它是一个 Series。
我的理解或方法有什么错误?
g = transactions.groupby(['Purchase_Type','year_month'])['Amount']
,但是使用groupby对象的原因是什么?期望的输出是什么? - jezrael['Amount'].sum()
?这样你就把你想要的对象丢掉了。 - John Zwinck