0

In the following code I want to export some columns in an excel file but I can't export the columns that are inside groupby, how can I do it.

import pandas as pd
df = pd.read_excel('ETATS_LOGS.xlsx',sheet_name = 'R9_17112020')

df1 = df.groupby(['webApp','mw'])['chgtCh','accessRecordModule','playerPlay startOver','playerPlay PdL','playerPlay PVR','contentHasAds','pdlComplete','lirePdl','lireVod'].sum()

print(df1)
df1.to_excel(r'logs.xlsx', index = False)

1 Answer 1

1

You can use reset_index() in order to get back the columns used for groupby if that's what you're asking.

Try:

import pandas as pd
df = pd.read_excel('ETATS_LOGS.xlsx',sheet_name = 'R9_17112020')

df1 = df.groupby(['webApp','mw'])['chgtCh','accessRecordModule','playerPlay startOver','playerPlay PdL','playerPlay PVR','contentHasAds','pdlComplete','lirePdl','lireVod'].sum()

df1.reset_index(inplace=True)

print(df1)
df1.to_excel(r'logs.xlsx', index = False)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, that's realy helped me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.