0

Hi I have datamodel like this:

Number    Type
123       A
123       B
455       B
455       A
455       C

and I need to make some kind of a pivot table like this:

Number    A    B    C    SUM
123       1    1    0    2
455       1    1    1    3

Tried with pandas pivot table but with no positive results

Thank you

BigBen
  • 46,229
  • 7
  • 24
  • 40
NC11
  • 3
  • 1

1 Answers1

1

You can do a crosstab then assign the count of unique Number to a new SUM column

out = (pd.crosstab(df['Number'], df['Type'])
       .assign(SUM=df['Number'].value_counts())
       .reset_index())
print(out)

Type  Number  A  B  C  SUM
0        123  1  1  0    2
1        455  1  1  1    3
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52