Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pandas.Dataframe.groupby without alphabetical ordering

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)
like image 553
anonymous Avatar asked Jul 29 '15 00:07

anonymous


People also ask

Does pandas Groupby keep order?

Groupby preserves the order of rows within each group. When calling apply, add group keys to index to identify pieces. Reduce the dimensionality of the return type if possible, otherwise return a consistent type.

Does group by sort data pandas?

Pandas Groupby is used in situations where we want to split data and set into groups so that we can do various operations on those groups like – Aggregation of data, Transformation through some group computations or Filtration according to specific conditions applied on the groups.

How do you sort values in Groupby pandas?

Sort Values in Descending Order with Groupby You can sort values in descending order by using ascending=False param to sort_values() method. The head() function is used to get the first n rows. It is useful for quickly testing if your object has the right type of data in it.

How do you get Groupby descending in pandas?

To group Pandas dataframe, we use groupby(). To sort grouped dataframe in descending order, use sort_values(). The size() method is used to get the dataframe size.


1 Answers

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)
like image 165
Def_Os Avatar answered Sep 19 '22 19:09

Def_Os