0

I have this dataframe loaded:

         ColA     ColB
0           3        3
1           4        5
2           4        5
3           5        4
4           5        3
5           5        3
6           4        3
7           5        4
8           5        4
9           3        4
10          5        5

And I want to count the number of times distinct pairs of data occur. For example the pair (5,4) should have a count of 3, (4,5) should have a count of 2, and so on. What I ultimately want is a third column whose row entry is the count of the number of times the pair in ColA, ColB occurs:

         ColA     ColB  Count
0           3        3      1
1           4        5      2
2           4        5      2 
3           5        4      3
4           5        3      2
5           5        3      2
6           4        3      1
7           5        4      3
8           5        4      3
9           3        4      1
10          5        5      1

I've tried creating a new column for the pairs using df['Pairs'] = (df['ColA'],df['ColB']), thinking that then I could use the .value_counts() method on the resulting column, but I get the error ValueError: Length of values does not match length of index.

Thanks for any insights you can provide.

EDIT: Clarified that I don't want to just view the counts, i want to incorporate them into the dataframe as a new column.

0 Answers0