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.

How to get equivalent of pandas.DataFrame.pivot_table

See original GitHub issue

How to get equivalent of pandas.DataFrame.pivot_table like here

table = pivot_table(df, values=['D', 'E'], index=['A', 'C'], aggfunc={'D': np.mean, 'E': [min, max, np.mean]})

Output:

                  D   E
               mean max median min
A   C
bar large  5.500000  16   14.5  13
    small  5.500000  15   14.5  14
foo large  2.000000  10    9.5   9
    small  2.333333  12   11.0   8

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
ashleydaviscommented, May 25, 2018

Hi, just to let you know I’ve upgraded the Data-Forge pivot function to support multiple outputs so that you can use it more like Pandas.

So now with Data-Forge 1.0.9 you can do your pivot like this:

const df2 = df1.pivot(["A", "C"], { 
    D: values => values.average(), 
    E: {
        min: values => values.min(),
        max: values => values.max(),
        mean: values => values.average()
    }
});

For an example please see the unit test pivot can produce multiple output values in the pivot tests file.

You also asked for multi-level indexes and columns (like Pandas), but at the moment I’m not planning on implementing this feature for Data-Forge. I’m afraid it’s going to stay in the too-hard basket for the forseeable future.

1reaction
ashleydaviscommented, May 26, 2018

Great!

You might also be interested know that I’m writing a book on this type of thing.

Please take a look: http://bit.ly/2t2cJu2

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Convert Pandas Pivot Table to DataFrame - Statology
You can use the following syntax to convert a pandas pivot table to a pandas DataFrame: df = pivot_name.reset_index().
Read more >
pandas.pivot_table — pandas 1.5.2 documentation
Create a spreadsheet-style pivot table as a DataFrame. The levels in the pivot table will be stored in MultiIndex objects (hierarchical indexes) on...
Read more >
A Guide to Pandas Pivot Table - Built In
Download or import the data that you want to use. · In the pivot_table function, specify the DataFrame you are summarizing, along with...
Read more >
pandas pivot table to data frame - python - Stack Overflow
Your original dataframe does not have header "patient". Where does it come from? – DYZ. Mar 10, 2017 at 1:19.
Read more >
Pandas Pivot Table Explained - Practical Business Python -
The simplest pivot table must have a dataframe and an index . In this case, let's use the Name as our index. pd.pivot_table(df...
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