Skip to content

Feature request Multi Index pivot #32129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kirialis opened this issue Feb 20, 2020 · 2 comments
Closed

Feature request Multi Index pivot #32129

kirialis opened this issue Feb 20, 2020 · 2 comments
Labels
Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@kirialis
Copy link

Hi, Would it be possible to implement pivot with a list of index ?

This will throw an error :

df.pivot(index=['time', 'category'], columns='bar', values='baz')

But this does : (Found on Stack Overflow)

def multiindex_pivot(df, index=None, columns=None, values=None):
    if index is None:
        names = list(df.index.names)
        df = df.reset_index()
    else:
        names = index
    list_index = df[names].values
    tuples_index = [tuple(i) for i in list_index] # hashable
    df = df.assign(tuples_index=tuples_index)
    df = df.pivot(index="tuples_index", columns=columns, values=values)
    tuples_index = df.index  # reduced
    index = pd.MultiIndex.from_tuples(tuples_index, names=names)
    df.index = index
    return df

df.pipe(multiindex_pivot, index=['time', 'category'], columns='bar', values='baz')

I just think it would be nice to have something directly available :)

@jreback
Copy link
Contributor

jreback commented Feb 20, 2020

this is already implemented for 1.1 if you’d like to find the issue / PR

@charlesdong1991
Copy link
Member

charlesdong1991 commented Feb 23, 2020

indeed, @kirialis , your description of feature request seems like being implemented in #30928 , and original issue is #21425 , could you please try to use master branch, and see if your issue resolved? If it is being solved, could close this issue then

FYI, another PR to add annotations for df.pivot is underway #32197

@charlesdong1991 charlesdong1991 added Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Feb 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

4 participants