python - I want to create a column of value_counts in my pandas dataframe -


i more familiar r wanted see if there way in pandas. want create count of unique values 1 of dataframe columns , add new column counts original data frame. i've tried couple different things. created pandas series , calculated counts value_counts method. tried merge these values original dataframe, keys want merge on in index(ix/loc). suggestions or solutions appreciated

color value red   100 red   150 blue  50 

and wanted return like

color value counts red   100   2 red   150   2  blue  50    1 

df['counts'] = df.groupby(['color'])['value'].transform('count') 

for example,

in [102]: df = pd.dataframe({'color': 'red red blue'.split(), 'value': [100, 150, 50]})  in [103]: df out[103]:    color  value 0   red    100 1   red    150 2  blue     50  in [104]: df['counts'] = df.groupby(['color'])['value'].transform('count')  in [105]: df out[105]:    color  value  counts 0   red    100       2 1   red    150       2 2  blue     50       1 

note transform('count') ignores nans. if want count nans, use transform(len).


to anonymous editor: if getting error while using transform('count') may due version of pandas being old. above works pandas version 0.15 or newer.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -