question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ENH] pivot_wider() and pivot_longer()

See original GitHub issue

Brief Description

Pandas has several functions for pivoting. The melt function takes a ‘wide’ dataframe and makes it ‘long’, and pivot_table takes a ‘long’ dataframe and makes it ‘wide’. Similarly, stack and unstack perform pivot operations using multilevel indices. Tom Augspurger explains all of the pandas pivoting functions in his Modern Pandas guide. If you look through his guide, you can see that the API for these four functions are all different and difficult to remember.

I would like to propose pivot_wider and pivot_longer functions inspired by functions in the R tidyr package. These functions are pure syntactic sugar around pandas melt and unpivot. They should have consistent APIs, and they should be symmetric. That is, you should be able to take a ‘wide’ dataframe, pass it through pivot_longer, then pass it through pivot_wider to get back to the exact same ‘wide’ dataframe that we started with.

Example API

Imagine that we have a ‘wide’ table of heartrate data for patients treated under two different drugs, a and b.

name a b
Wilbur 67 56
Petunia 80 90
Gregory 64 50

Here’s how we would convert this to a ‘long’ table.

df.pivot_longer(column_names=['a', 'b'], names_to=['drug'], values_to=['heartrate'])

The output would be a ‘long’ table that look like this.

name drug heartrate
Wilbur a 67
Petunia a 80
Gregory a 64
Wilbur b 56
Petunia b 90
Gregory b 50

Now we’ll take the above table and transform it back into a ‘wide’ table using pivot_wider.

df.pivot_wider(names_from=['drug'], values_from=['heartrate'])

And now we’re back to our ‘wide’ table:

name a b
Wilbur 67 56
Petunia 80 90
Gregory 64 50

I would love some feedback on the API before I start implementing this in code!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ericmjlcommented, Sep 13, 2020
1reaction
ericmjlcommented, Sep 10, 2020

@samukweku assigning! If possible, please use as much as you can salvage from @benjaminjack’s PR, and include him in the changelog entry! Sharing the work/credit keeps things encouraging for everyone involved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pivot data from wide to long — pivot_longer • tidyr
pivot_longer() "lengthens" data, increasing the number of rows and decreasing the number of columns. The inverse transformation is pivot_wider().
Read more >
Tidyr's pivot_longer() and pivot_wider() Examples From the ...
An example of how pivot_wider() works from this week's #TidyTuesday data set. First, let's start with a toy data set that illustrates these...
Read more >
[ENH] pivot_wider() and pivot_longer() · Issue #434 - GitHub
I would like to propose pivot_wider and pivot_longer functions inspired by functions in the R tidyr package. These functions are pure syntactic sugar...
Read more >
6 Advanced pivoting - Data Wrangling
pivot_longer() and pivot_wider() are very flexible, and can easily tidy a wide variety of non-tidy datasets. The previous chapter only covered the basics....
Read more >
Never look up tidyr's pivot_wider or pivot_longer again!
The RStudio code snippet below will generate reusable fill-in-the-blank code and explanations for each pivot_wider() argument. snippet pwider
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found