24

I have a dataframe that I want to alter (according to the code right below) but it put's all the 'Experiment' name values in alphabetical order. Is there a way to leave the order as it is after calling pandas.Dataframe.groupby?

df = df.groupby(['Experiment', 'Step'], as_index=False)['value'].aggregate(np.sum)
anonymous
  • 815
  • 3
  • 13
  • 21

1 Answers1

44

groupby takes a keyword argument sort, which by default is True. You should do:

df = df.groupby(['Experiment', 'Step'], sort=False, as_index=False)['value'].aggregate(np.sum)
Def_Os
  • 5,301
  • 5
  • 34
  • 63
  • Is it possible to pass the sort list? I mean, I'd like to tell pandas what is the sort rule. – Sigur Apr 01 '21 at 18:05