python - Fastest way to get occurrence of each element -
i have large dataframe looking
name country ... 1 paul germany 2 paul germany 3 george italy 3 george italy 3 george italy ... n john usa i'm looking occurence of each element of name column, such has
name country count 1 paul germany 2000 2 george italy 500 ... n john usa 40000 any idea optimal way ?
because quite long
df['count'] = df.groupby(['name'])['name'].transform(pd.series.value_counts)
you can this:
df.groupby(['name', 'country']).size() example:
import pandas pd df = pd.dataframe.from_dict({'name' : ['paul', 'paul', 'george', 'george', 'george'], 'country': ['germany', 'italy','germany','italy','italy']}) df output:
country name 0 germany paul 1 italy paul 2 germany george 3 italy george 4 italy george group , count:
df.groupby(['name', 'country']).size() output:
name country george germany 1 italy 2 paul germany 1 italy 1
Comments
Post a Comment