0

I'm running python 3.X on databricks & I'm doing a simple group by operation in pandas using following code

d={'Age_class1': 'mean', 'Age_class2': 'mean','Age_class3': 'mean','Age_class4': 'mean','Attendance': 'count'}
df_student14=df_student.groupby(['state','school','Age_class1']).agg(d)

Then I'm saving this file in csv using df_student14.to_csv(filename) My csv file is showing following fields

state, school,Age_class1,Age_class1,Age_class2,Age_class3,Age_class4,Attendance

But df_student14.columns is showing following output

Index(['Age_class1', 'Age_class2', 'Age_class3', 'Age_class4', 'Attendance'], dtype='object')

Can you let me know how do I get corresponding 'state','school','Age_class1' in my df_student14 data?

newinPython
  • 313
  • 1
  • 6
  • 19
  • 2
    add reset_index() at the end – BENY Jul 29 '20 at 14:08
  • Does this answer your question? [pandas groupby without turning grouped by column into index](https://stackoverflow.com/questions/32059397/pandas-groupby-without-turning-grouped-by-column-into-index) – MrNobody33 Jul 29 '20 at 14:27
  • @YOBEN_S @MrNobody,it's partly solving my problem. Yes I can see state & school in my data frame if I'm not considering Age_class1 in group by.But my business logic need Age_class1 in group by & if I'm considering it then I'm getting Error message `cannot insert Age_Class1, already exists` I'm thinking to concatenate Age_Class1 with school in group by & then separate it after group by. Let me know if you have any better approach – newinPython Jul 29 '20 at 14:40
  • Can you provide some example data? – ttreis Jul 29 '20 at 15:08

0 Answers0