Skip to content

Instantly share code, notes, and snippets.

@jtemporal
Created January 31, 2019 19:27
Show Gist options
  • Save jtemporal/5e601921f7c900643b9ddc220754eaba to your computer and use it in GitHub Desktop.
Save jtemporal/5e601921f7c900643b9ddc220754eaba to your computer and use it in GitHub Desktop.
import pandas as pd
import numpy as np
raw_data = {'id': [1,2,1,1,3,4], 'categoria': ['A', 'B', 'A', 'B', 'C', np.nan]}
t = pd.DataFrame(raw_data, columns=['id', 'categoria'])
def cat_mais_freq(user):
value_count_index = user['categoria'].value_counts().index
if value_count_index.empty:
return None
return value_count_index.values[0]
# t
t.groupby('id').apply(cat_mais_freq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment