Transform columns into python panda lines

0

I need to make some modifications to a dataframe but I can not. I'm using pandas.

I have the table below:

ButdoIwantittolooklikethis?

Can anyone help me?

    
asked by anonymous 27.04.2017 / 20:38

2 answers

2

Good afternoon, you can do this using the command melt

Here is the test I did:

Import input data

import pandas
data = pandas.DataFrame({'Local':['são paulo','rio','bahia'], '01/09/2007': [1,2,0], '01/10/2007': [0,1,0], '01/11/2007': [2,1,1], '01/12/2007': [2,0,3], '01/01/2008': [0,1,1], 'contato':['2345-1244','5422-1244','2345-8674']})

Applies the melt command to the indexes, setting Local and contato to ids and adding the mes variable to the value quantidade within the new dataFrame, and I already sort by Location and quantity in descending order

data = pandas.melt(data.reset_index(), id_vars=['Local', 'contato'], 
var_name='mes', value_name='quantidade').sort(['Local','quantidade'], ascending=False)

The new dataFrame has been created, but we still have some undesirable lines that contain index in value, so I simply remove these lines

data = data[data.mes.str.contains("index") == False]

The result follows

I hope I have helped Hugs

    
27.04.2017 / 22:14
-1

Hello, all right, I found a pretty cool solution, which solved the problem I'll post the image here !, df.stack ()

    
28.12.2018 / 14:11