问题
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)
回答1:
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)
来源:https://stackoverflow.com/questions/31689149/using-pandas-dataframe-groupby-without-alphabetical-ordering