0

I need some help

I have the follow CSV file with this Data Frame:

enter image description here

how could I transfer the data of cases in columns week 1, week 2 (...) using Python and Pandas? It would be something like this:

enter image description here

1 Answers1

0
x = (
    df.pivot_table(
        index=["city", "population"],
        columns="week",
        values="cases",
        aggfunc="max",
    )
    .add_prefix("week ")
    .reset_index()
    .rename_axis("", axis=1)
)
print(x)

Prints:

  city  population  week 1  week 2
0    x       50000       5      10
1    y       88000       2      15
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91