Using pandas.Dataframe.groupby without alphabetical ordering

anonymous picture anonymous · Jul 29, 2015 · Viewed 8.8k times · Source

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)

Answer

Def_Os picture Def_Os · Jul 29, 2015

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)