Pandas DataFrameGroupBy 导出到 Excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25789264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas DataFrameGroupBy export to Excel
提问by sstevan
I'm trying to export the count of grouped records to Excel.
我正在尝试将分组记录的计数导出到 Excel。
I have CSV file that looks like this:
我有一个看起来像这样的 CSV 文件:
Date Country Sub Source
2014-09-11 US 1 source1
2014-09-11 US 2 source2
2014-09-11 UK 1 source3
2014-09-11 US 1 source1
2014-09-11 IN 3 source4
I need the grouped count of records by Country, Sub and Source.
我需要按 Country、Sub 和 Source 分组的记录数。
df = pd.read_csv('log.csv',sep='\t')
count = df.groupby(['Country','Sub','Source']).size()
count.to_excel('report.xls', index=False)
But I get an error when I run it: AttributeError: 'Series' object has no attribute 'to_excel'
但是当我运行它时出现错误: AttributeError: 'Series' object has no attribute 'to_excel'
Is exporting a DataFrameGroupBy to Excel not allowed? Also is there a way to insert grouped records with the count info into a MySQL database?
是否不允许将 DataFrameGroupBy 导出到 Excel?还有一种方法可以将带有计数信息的分组记录插入到 MySQL 数据库中吗?
采纳答案by Ashoka Lella
回答by cdarlint
df = pd.read_csv('log.csv',sep='\t')
df = pd.DataFrame(df.groupby(['Country','Sub','Source']).size(),columns=['Count'])
You'll get:
你会得到:
Count
Country Sub Source
IN 3 source4 1
UK 1 source3 1
US 1 source1 2
2 source2 1
then save to excel by:
然后通过以下方式保存到excel:
df.to_excel('output.xls')

