I'm trying to export the count of grouped records to Excel.
I have CSV file that looks like this:
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.
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'
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?
According to the docs groupby returns a GroupBy object. The to_excel operation can only be performed on DataFrame
. You can cast the GroupBy object to a DataFrame
and then call the to_excel
function