Python – Pairwise count items

Try this:

b['col3'] = (b['col1'] + '-' + b['col2'])
print(b.groupby('col3').size())

Output:

a-b    2
b-a    2
c-d    1
d-c    1

EDIT 1

Based on your input data (as in comments), here is the df I made up and the results

Code:

df[['seller_city','customer_city']]

Output:

            seller_city         customer_city
0         volta redonda  sao jose dos pinhais
1         volta redonda  sao jose dos pinhais
2  sao jose dos pinhais         volta redonda

Code:

df.groupby((df['seller_city'] + '-' + df['customer_city'])).size()

Output:

sao jose dos pinhais-volta redonda    1
volta redonda-sao jose dos pinhais    2

Leave a Comment