0

I have a csv, which looks like this :

    location         date        people_vaccinated
0   Afghanistan   2021-02-22          1225
1   Afghanistan   2021-02-23          1145
2   Afghanistan   2021-02-24          2212
3   Algeria       2021-02-22          1120
4   Algeria       2021-02-23          1824
5   Algeria       2021-02-24          1998

I need the following:

                 2021-02-22  2021-02-23  2021-02-24
   Afghanistan      1225        1145        2212
   Algeria          1120        1824        1998
    

Tried with 'transpose' and 'stack' but it does not exactly what I want.

Delacroix
  • 49
  • 1
  • 8

1 Answers1

1

Using pivot:

res = df.pivot(index='location', columns=['date'], values=['people_vaccinated'])
##### to convert in the given format ####
res.columns = res.columns.droplevel()
res.columns.name = ''
res = res.reset_index()

    location        2021-02-22  2021-02-23  2021-02-24
0   Afghanistan     1225        1145        2212
1   Algeria         1120        1824        1998
Pygirl
  • 12,969
  • 5
  • 30
  • 43