w3resource

Pandas Series: groupby() function

Splitting the object in Pandas

The groupby() function involves some combination of splitting the object, applying a function, and combining the results.

This can be used to group large amounts of data and compute operations on these groups

Syntax:

Series.groupby(self, by=None, axis=0, level=None, as_index=True, sort=True, group_keys=True, squeeze=False, observed=False, **kwargs)
Pandas Series groupby image

Parameters:

Name Description Type/Default Value Required / Optional
by Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align() method). If an ndarray is passed, the values are used as-is determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted as a (single) key. mapping, function, label, or list of labels Required
seriesaxis  Split along rows (0) or columns (1). {0 or ‘index’, 1 or ‘columns’}
Default Value : 0
Required
level If the axis is a MultiIndex (hierarchical), group by a particular level or levels. int, level name, or sequence of such
Default Value : None
Required
as_index  For aggregated output, return object with group labels as the index. Only relevant for DataFrame input. as_index=False is effectively “SQL-style” grouped output. bool
Default Value : True
Required
sort Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. Groupby preserves the order of rows within each group. bool
Default Value : True
Required
group_keys When calling apply, add group keys to index to identify pieces. bool
Default Value : True
Required
squeeze Reduce the dimensionality of the return type if possible, otherwise return a consistent type. bool
Default Value : False
Required
observed This only applies if any of the groupers are Categoricals. If True: only show observed values for categorical groupers. If False: show all values for categorical groupers. bool
Default Value : False
Required
**kwargs Only accepts keyword argument ‘mutated’ and is passed to groupby.   Optional

Returns: DataFrameGroupBy or SeriesGroupBy
Depends on the calling object and returns groupby object that contains information about the groups.

Example:

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'Animal': ['Tiger', 'Tiger',
                              'Dog', 'Dog'],
                   'Max Speed': [270., 260., 36., 32.]})
df

Output:

  Animal	Max Speed
0	Tiger	270.0
1	Tiger	260.0
2	 Dog	36.0
3	 Dog	32.0
Pandas Series groupby image

Python-Pandas Code:

import numpy as np
import pandas as pd
df = pd.DataFrame({'Animal': ['Tiger', 'Tiger',
                              'Dog', 'Dog'],
                   'Max Speed': [270., 260., 36., 32.]})
df.groupby(['Animal']).mean()

Output:

       Max Speed
Animal	
Dog	   34.0
Tiger	  265.0

Example - Hierarchical Indexes:

We can groupby different levels of a hierarchical index using the level parameter:

Python-Pandas Code:

import numpy as np
import pandas as pd
arrays = [['Tiger', 'Tiger', 'Dog', 'Dog'],
          ['Captive', 'Wild', 'Captive', 'Wild']]
index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))
df = pd.DataFrame({'Max Speed': [280., 250., 30., 20.]},
                  index=index)
df

Output:

                  Max Speed
Animal Type              
Tiger  Captive      280.0
       Wild         250.0
Dog    Captive       30.0
       Wild          20.0

Python-Pandas Code:

import numpy as np
import pandas as pd
arrays = [['Tiger', 'Tiger', 'Dog', 'Dog'],
          ['Captive', 'Wild', 'Captive', 'Wild']]
index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))
df = pd.DataFrame({'Max Speed': [280., 250., 30., 20.]},
                  index=index)
df.groupby(level=0).mean()

Output:

                  Max Speed
Animal	
Dog	            25.0
Tiger	           265.0

Python-Pandas Code:

import numpy as np
import pandas as pd
arrays = [['Tiger', 'Tiger', 'Dog', 'Dog'],
          ['Captive', 'Wild', 'Captive', 'Wild']]
index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))
df = pd.DataFrame({'Max Speed': [280., 250., 30., 20.]},
                  index=index)
df.groupby(level=1).mean()

Output:

           Max Speed
Type	
Captive	   155.0
Wild	     135.0

Previous: Map values of Pandas Series
Next: Rolling window calculations in Pandas



Share this Tutorial / Exercise on : Facebook and Twitter