Count by unique pair of columns in pandas

print(d.groupby(['ip', 'useragent']).size().reset_index().rename(columns={0:''}))

gives:

            ip useragent   
0  192.168.0.1         a  2
1  192.168.0.1         b  1
2  192.168.0.2         b  1

Another nice option might be pandas.crosstab:

print(pd.crosstab(d.ip, d.useragent) )
print('\nsome cosmetics:')
print(pd.crosstab(d.ip, d.useragent).reset_index().rename_axis('',axis='columns') )

gives:

useragent    a  b
ip               
192.168.0.1  2  1
192.168.0.2  0  1

some cosmetics:
            ip  a  b
0  192.168.0.1  2  1
1  192.168.0.2  0  1

If you use groupby, you will get what you want.

d.groupby(['ip', 'useragent']).size()

produces:

ip          useragent               
192.168.0.1 a           2
            b           1
192.168.0.2 b           1

Tags:

Python

Pandas