Python Forum

Full Version: Help in pandas group by
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have below dataframe:-
df=pd.DataFrame({'a':[1,1,1,1,2,2,2,2],'b':[1,1,2,2,2,6,7,7]})
a b
1 1
1 1
1 2
1 2
2 2
2 6
2 7
2 7

I want to create a new column called say "cnt", so that it gives count of unique b for same a. For ex:- in above case "cnt" would be like

a b cnt
1 1 1
1 1 1
1 2 2
1 2 2
2 2 1
2 6 2
2 7 3
2 7 3