Skip to content
Advertisement

Python/Excel/SQL column dates to exploded rows

I have an Excel spreadsheet table like this where it shows how many units were sold each week for 2019 and 2020 (columns) and I am trying to put it into an access database formatted like the second table:

and I want to get it like this

What’s the best way to do this in Python (pandas) or VBA/Excel formula (or SQL if need be)?

Advertisement

Answer

Just need to be systematic

  1. row index set_index("model")
  2. name column series df.columns.set_names("weekencoded", inplace=True)
  3. then you are ready to stack()
  4. rename column to units, reset_index() so encoded string is available as columns
  5. parse out the values you want from encoded string
  6. I’ve left it for you to convert a week number to a month number

output

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement