Using pandas.Dataframe.groupby without alphabetical ordering

吃可爱长大的小学妹 提交于 2019-12-10 13:25:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!