groupby关于“COL1”,然后应用
transform上
Col2返回了一系列的指数对准原来的DF这样你就可以将其添加为列:
In [3]:df['Occur'] = df.groupby('Col1')['Col2'].transform(pd.Series.value_counts)dfOut[3]: Col1 Col2 Occur0 test Something 21 test2 Something 22 test3 Something 13 test Something 24 test2 Something 25 test5 Something 1


