Home > Blockchain >  Unmerge cells when using groupby (PANDAS)
Unmerge cells when using groupby (PANDAS)

Time:08-24

I grouped some data using groupby:

df1['N° of options'] = df.groupby(['Country','Restaurant']).Food.size()

And the result is a dataframe with grouped arguments merged, instead of it I'd like to repeat these values along the cells.

Any clue about how can I display data like this? For now, I got something like this:

enter image description here

Thank you!!

CodePudding user response:

Assuming that grouped_df is your grouped dataframe, you can use enter image description here

>>> print(grouped_df.reset_index())

enter image description here

Another way to do it is to add as_index=False argument to your groupyby clause :

grouped_df = df.groupby(['SG_UF', 'SG_PARTIDO'], as_index=False).sum()

CodePudding user response:

If I understand correctly, you are trying to sort instead of groupby as you have mentioned you want to see the values.

sort works like df_name.sort_values(by column_name, axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’, ignore_index=False, key=None)

In your code, it could look like:

df.sort_values(by = ['Country','Restaurant']). Use other arguments as required, like, order of sort, etc.

  • Related