Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pivoting DataFrame in Python Pandas

I’m trying to pivot my df from wide to long, and I am attempting to replicate R’s dplyr::pivot_longer() function. I have tried pd.wide_to_long() and pd.melt() but have had no success in correctly formatting the df. I also attempted using df.pivot() and come to the same conclusion.

Here is what a subset of the df (called df_wide) looks like: Rows are Store Numbers, Columns are Dates, Values are Total Sales

Subset of df_wide

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My current function looks like this:

df_wide.pivot(index = df_wide.index, 
              columns = ["Store", "Date", "Value"], # Output Col Names
              values = df_wide.values) 

My desired output is a df that looks like this:

Desired output

  • Note – this question is distinct from merging, as it is looking at changing the structure of a single data frame

>Solution :

The stack() function is useful to achieve your objective, then reformat as needed:

pd.DataFrame( df.stack() ).reset_index(drop=False).rename(columns={'level_0':'store', 'level_1':'Date', 0:'Value'})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading