-1

I have few doubts with subsetting grouping the data.

My actual data format looks like this

    month       userId     usage_count    userEmail
   January  aabzhlxycj         2    jakiyah@academy.com
   January  aacuvynjwq         1    jack@gmail.com
   December aabzhlxycj         2    jakiyah@academy.com
   January  aailjxciyk         2    maria@gmail.com
   December aacuvynjwq         1    jack@gmail.com

I need to convert this above data to this format

UserId      userEmail                    January    December
aabzhlxycj  jakiyah@academy.com             2          2 
aacuvynjwq  jack@gmail.com                  1          1
aailjxciyk  maria@gmail.com                 2          0

Can anyone please suggest to get the data in this above format.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
sangeetha
  • 53
  • 1
  • 9

1 Answers1

0

You can use a pivot table:

import pandas as pd

result = pd.pivot_table(df, values="usage_count", index=["userId", "userEmail"], columns="month").fillna(0).reset_index()
print(result)

Output:

month      userId            userEmail  December  January
0      aabzhlxycj  jakiyah@academy.com       2.0      2.0
1      aacuvynjwq       jack@gmail.com       1.0      1.0
2      aailjxciyk      maria@gmail.com       0.0      2.0
Dan
  • 1,575
  • 1
  • 11
  • 17