0

I am new to python programming.

I want to loop through each user and get unique countries and its counts.

Here's the input data i have:

df = pd.DataFrame({'User':['101','101','102','102','102'],'Country':['India,Brazil','India','India,Brazil,Japan','India,Brazil','India']})

wanted output here

wanted output

Thanks

Kumar AK
  • 987
  • 3
  • 10
  • 23
  • 2
    The duplicate doesn't particularly answer the question, but there is a dupe. Anyway, use `df.set_index('User').Country.str.split(',', expand=True).stack().groupby(level=0).value_counts()` – cs95 Jun 05 '18 at 16:14
  • Thanks so much @coldspeed . if you dont mind how to repeat the user id value to all the rows? – Kumar AK Jun 05 '18 at 16:24
  • 1
    Add `.reset_index()` – cs95 Jun 05 '18 at 16:30
  • thanks..df.set_index('User id').Country.str.split(',', expand=True).stack().groupby(level=0).value_counts().reset_index().rename(columns = {'level_1' : 'Country',0:'ctry_count'}) is there way to avoid rename statement here? – Kumar AK Jun 05 '18 at 17:04

0 Answers0