-1

I have this DataFrame:

    user_id     ip
 0        2       106.184.2.137
 1        2       106.184.2.137
 2        3       106.184.2.113
 3        3       106.184.2.112
 4        9       103.105.111.191
 5        9       103.105.111.191
 6       11       103.239.29.190

How can I get the number of unique ip addresses per user id as follows:

    user_id     ip_num
 0        2       1
 1        3       2
 2        9       1
 3        11      1

thank you for your help.

ayhan
  • 70,170
  • 20
  • 182
  • 203
xingyuan jiang
  • 153
  • 1
  • 4

1 Answers1

1

Like this:

df = df.groupby('user_id')['ip'].nunique().reset_index().rename(columns={'ip': 'ip_num'})
zipa
  • 27,316
  • 6
  • 40
  • 58