Skip to content

Instantly share code, notes, and snippets.

@samukweku
Last active August 2, 2021 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samukweku/c7ee82e70e0cabce48e3598226577901 to your computer and use it in GitHub Desktop.
Save samukweku/c7ee82e70e0cabce48e3598226577901 to your computer and use it in GitHub Desktop.
Pandas Chaining - Second most common value

link

import pandas as pd
df = pd.DataFrame({'x': [1, 1, 1, 2, 2, 3],
                   'y': [1, 2, 3, 1, 2, 1],
                   'n': [3, 2, 1, 1, 2, 1]})

df
x y n
0 1 1 3
1 1 2 2
2 1 3 1
3 2 1 1
4 2 2 2
5 3 1 1
# wrap the function into pipe
# a handy way to chain functions 
# pipe can return arbitrary objects
# and can be handy in some awkward pandas operations

df.groupby('x').y.pipe(lambda df: pd.DataFrame({'frequency' : df.sum(), 
                                                'second' : df.nth(1)}
                                              )
                      )
frequency second
x
1 6 2.0
2 3 2.0
3 1 NaN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment