5.6 groupby#

This lesson shows the usage of groupby in pandas

Important

This lesson is still under development.

The material in this lesson is mostly inspired from pandas documentation for groupby and realpython However, it has been modified for easy of practice and understanding.

import pandas as pd

print(pd.__version__)
1.5.3
df = pd.DataFrame( {'a':['A','A','B','B','B','C'], 'b':[1,2,5,5,4,6]})

df
a b
0 A 1
1 A 2
2 B 5
3 B 5
4 B 4
5 C 6


df.groupby('a')['b'].apply(list)
a
A       [1, 2]
B    [5, 5, 4]
C          [6]
Name: b, dtype: object
df.groupby('a')['b'].apply(list).reset_index(name='new')
a new
0 A [1, 2]
1 B [5, 5, 4]
2 C [6]


Total running time of the script: ( 0 minutes 0.008 seconds)

Gallery generated by Sphinx-Gallery