Home > Mobile >  python groupby to dataframe (just groupby to data no additional functions) to export to excel
python groupby to dataframe (just groupby to data no additional functions) to export to excel

Time:09-26

I am at a total loss as to why this is impossible to find but I really just want to be able to groupby and then export to excel. Don't need counts, or sums, or anything else and can only find examples including these functions. Tried removing those functions and the whole code just breaks.

Anyways:

Have a set of monthly metrics - metric name, volumes, date, productivity, and fte need. Simple calcs got the data looking nice, good to go. Currently it is grouped in 1 month sections so all metrics from Jan are one after the other etc. Just want to change the grouping so first section is individual metrics from Jan to Dec and so on for each one.

Initial data I want to export to excel (returns not a dataframe error)

dfcon = pd.concat([PmDf,ReDf])

dfcon['Need'] = dfcon['Volumes'] / (dfcon['Productivity']*21*8*.80)

dfcon[['Date','Current Team','Metric','Productivity','Volumes','Need']]

dfg = dfcon.groupby(['Metric','Date'])

dfg.to_excel(r'S:\FilePATH\GroupBy.xlsx', sheet_name='pandas_group', index = 0)

The error I get here is: 'DataFrameGroupBy' object has no attribute 'to_excel' (I have tried a variety of conversions to dataframes and closest I can get is a correct grouping displaying counts only for each one, which I do not need in the slightest)

I have also tried:

dfcon.sort('Metric').to_excel(r'S:\FILEPATH\Grouped_Output.xlsx', sheet_name='FTE Need', index = 0)

this returns the error: AttributeError: 'DataFrame' object has no attribute 'sort'

Any help you can give to get this to be able to be exported grouped in excel would be great. I am at my wits end here after over an hour of googling. I am also self taught so feel like I may be missing something very, very basic/simple so here I am!

Thank you for any help you can provide!

Ps: I know I can just sort after in excel but would rather learn how to make this work in python!

CodePudding user response:

I am pretty sure sort() doesnt work anymore, try sort_values()

  • Related