在不按字母顺序使用pandas.Dataframe.groupby的情况下 [英] Using pandas.Dataframe.groupby without alphabetical ordering
本文介绍了在不按字母顺序使用pandas.Dataframe.groupby的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要更改的数据框(根据下面的代码),但是所有实验"名称值均按字母顺序排列.调用 pandas.Dataframe.groupby 后,是否可以保持订单原样?
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)
推荐答案
groupby
采用关键字参数sort
,默认情况下为True
.您应该这样做:
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)
这篇关于在不按字母顺序使用pandas.Dataframe.groupby的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文