Turn columns into rows into a dataframe

Asked

Viewed 3,726 times

1

I have a dataframe with the columns:

LUC
Contrato
Fantasia
Ano
Mes
01
02
03
05
...
30
Tatividade

I need to turn these columns with the days of the month into rows. In each column has the value sold that day by the store. It would have to look like this:

LUC
Contrato
Fantasia
Ano 
mes
dia
valor

inserir a descrição da imagem aqui

I tried the code:

vendas = pd.melt(vendas.reset_index(), id_vars=['LUC', 'Contrato','Fantasia','ano','mes','Tatividade'], 
var_name='dia', value_name='valor')

The columns were created, but the values were incorrect. In the day column was the index value and in the value column was a sequence.

2 answers

1

Tried to transpose the dataframe?

It does the inversion between rows and columns.

pd = pd.T

0

Dude, have you tried breaking up this operation reset_index()?

Test this solution:

vendas = pd.melt(vendas, 
                 id_vars=['LUC','Contrato','Fantasia','ano','mes','Tatividade'], 
                 var_name='dia', 
                 value_name='valor')
vendas.reset_index(drop=True, inplace=True)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.