I have to following df:
Col1 Col2
test Something
test2 Something
test3 Something
test Something
test2 Something
test5 Something
I want to get
Col1 Col2 Occur
test Something 2
test2 Something 2
test3 Something 1
test Something 2
test2 Something 2
test5 Something 1
I've tried to use:
df["Occur"] = df["Col1"].value_counts()
But it didn't help. I've got Occur column full of 'NaN'
You can also use
GroupBy
+transform
withsize
:groupby
on 'col1' and then applytransform
onCol2
to return a Series with its index aligned to the original df so you can add it as a column:You can also do:
So then:
Is: