I have a dataframe like this:
df = pd.DataFrame(
columns=['a', 'b'],
data={
'a' : [1, 1, 1, 2, 2, 2, 3, 3, 3],
'b' : [1, 2, 2, 1, 2, 3, 4, 4, 4]
}
)
How can I add a column named c
containing number of same pairs of a
and b
in df
:
c = [1, 2, 2, 1, 1, 1, 3, 3, 3]
Pair a=1
b=1
is present 1 time, a=1
b=2
two times, a=2
b=1
one time, a=3
b=4
three times and so on.