Understanding the Groupby Function in Pandas

Hongtao Hao / 2021-07-10


Some key points #

  • Suppose you have a DataFrame named as df, then df.groupby('param') will create multiple groups based on the parameter you provide.

  • Each group is a DataFrame.

  • For each group, group[0] is the string of param, whereas group[1] is the group’s actual data in the form DataFrame.

How to get a specific group’s data #

dfGroupby = df.groupby('param')
dfGroupby.get_group("GROUP_NAME")

How to print a groupby object #

Question is posted here: https://stackoverflow.com/questions/22691010/how-to-print-a-groupby-object

QPeiran provides a clever solution , which later is improved by set92:

df.groupby('param').apply(display)

Gajraj Singh Chouhan mentions that you need to import display from Ipython.display, but I am not sure whether it’s necessary. I didn’t import it and didn’t experience any problem.

Last modified on 2021-10-24