0

I have a pandas dataframe as follows.

time          location         count
2022-05-01       A              1
2022-05-01       B              2
2022-05-01       C              3
2022-05-01       D              4
2022-05-02       A              5
2022-05-02       B              6
2022-05-02       C              7
2022-05-02       D              8

I want to transform the dataframe as below.

time          A     B     C     D
2022-05-01    1     2     3     4
2022-05-02    5     6     7     8
matcha latte
  • 185
  • 1
  • 12

1 Answers1

1
df.pivot(index='time', columns='location', values='count')

location    A  B  C  D
time                  
2022-05-01  1  2  3  4
2022-05-02  5  6  7  8
jch
  • 3,600
  • 1
  • 15
  • 17