Skip to content
Advertisement

CSV data preprocess

I have a .csv file like this format

enter image description here

Then I want to convert it to

enter image description here

How can I do it with python pandas

Thank you

Advertisement

Answer

If you load it then you will have DataFrame like

JavaScript

Set multi-index usinig year and month

JavaScript
JavaScript

Reshape it using stack()

JavaScript
JavaScript

You can add name to column with day

JavaScript
JavaScript

Reset index to get normal columns

JavaScript
JavaScript

Create column with dates

JavaScript
JavaScript

You can remove year, month, day

JavaScript
JavaScript

You can also rename column

JavaScript
JavaScript

And you can change order of columns

JavaScript
JavaScript

Minimal working code

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