Skip to content
Advertisement

Turn multiple columns into two new columns in a dataframe using Pandas

I am working in a python pandas environment :D

Currently, I have a dataframe that looks like this :

JavaScript

My goal is to make the dataframe look like this :

JavaScript

Basically, I want the last 9 column titles and values to become their own rows on 2 new columns while keeping the first 8 columns and rows the same. I am aware that this means the data will be duplicated.

I saw some other answers on stackoverflow use the following code for smaller dataframes but it hasn’t worked for me :

JavaScript

Any and all help is appreciated ! Thank you

Advertisement

Answer

You are almost there with melt

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