Kyle Walden
Kyle Walden

Reputation: 115

Python Pivot 550+ Columns

I have a dataframe with 558 columns in it. The first 12 columns are monthly sales, and the rest of the columns are characteristics of a product. Each row of the dataframe represents a single product.

I want to pivot the table so that the 12 sales columns for each product are representative rows for the product i.e. each product then will have 12 rows.

I thought the following code would work:

df.pivot(index=df[df.columns[0:11]] , columns=df[df.columns[12:558]])

But, no luck.

Appreciate any help!

Upvotes: 0

Views: 44

Answers (1)

Kyle Walden
Kyle Walden

Reputation: 115

Solution: df = df.melt(id_vars = [*df.columns[-546:]])

Upvotes: 1

Related Questions