user5844628
user5844628

Reputation: 419

Pandas aggregating values of multiple columns

Here is an example of my DataFrame

  user    workflow    device     location  date 
  A         top         Mac      SF        1/1/19
  B         left        Win      SA        1/1/19
  A         right       Mac      SF        1/5/19
  C         left        iphone   LA        1/7/19
  D         left        pixel    BO        1/20/19
  C         bottom      iphone   LA        1/21/19

I want to transform the DF to look like this:

 User top bottom left right Mac Win iphone pixel SF  SA   LA  BO
 A     1   0     0      1   1   0     0     0     2   0   0    0
 B     0   0     1      0   0   1     0     0     0   1   0    0
 C     0   1     1      0   0   0     2     0     0   0   2    0
 D     0   0     1      0   0   0     0     1     0   0   0    1

How would one do that using Pandas?

Upvotes: 0

Views: 37

Answers (1)

BENY
BENY

Reputation: 323396

Using str.get_dummies after stack

s=df.set_index('user').stack().str.get_dummies().sum(level=0)
s  
      BO  LA  Mac  SA  SF  Win  bottom  iphone  left  pixel  right  top
user                                                                   
A      0   0    2   0   2    0       0       0     0      0      1    1
B      0   0    0   1   0    1       0       0     1      0      0    0
C      0   2    0   0   0    0       1       2     1      0      0    0
D      1   0    0   0   0    0       0       0     1      1      0    0

Upvotes: 1

Related Questions