Skip to content

ENH: pct_change in a group by return groups #53739

Closed
@marcdelabarrera

Description

@marcdelabarrera

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

I have a dataset with date, industry, wage and price. I want to compute the percentage increase in wages and prices by industry. I can do something like:

data.set_index('date').groupby('industry')[['wage','price']].pct_change()

But this will return me a pandas dataframe with date as an index and wage and price as columns, losing the industry column.

Feature Description

It would be nice to add an option to pct_change(), when applied to a groupby object, to also return the groups, maybe as an index as the agg function does.

Alternative Solutions

My current approach is:
pd.concat([data.set_index('date')[['industry']], data.set_index('date').groupby(['industry'])[['wage','price']].pct_change()],axis=1)

Not a big deal but seems a common operation. Having a grouped dataframe and wanting to compute percentage changes for several columns.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions