-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    [email protected]
   January  aacuvynjwq         1    [email protected]
   December aabzhlxycj         2    [email protected]
   January  aailjxciyk         2    [email protected]
   December aacuvynjwq         1    [email protected]

I need to convert this above data to this format

UserId      userEmail                    January    December
aabzhlxycj  [email protected]             2          2 
aacuvynjwq  [email protected]                  1          1
aailjxciyk  [email protected]                 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  [email protected]       2.0      2.0
1      aacuvynjwq       [email protected]       1.0      1.0
2      aailjxciyk      [email protected]       0.0      2.0
Dan
  • 1,575
  • 1
  • 11
  • 17