Skip to content

Accessing unused columns in pivot_table #4733

@wabee

Description

@wabee

related is #6490

I am new to pandas but have run into a problem which I believe is a bug.

I tried to execute the following:

df_sum=pd.pivot_table(df,rows=['Konskund_MEAB','ProdID'],cols=['Year'], aggfunc=lambda x: np.average(x['snittpris'],
weights=x['Antal_forsandelser'])

This produce a number of errors.

The following commands produce the desired results:

rows = ['Konskund_MEAB','ProdID']
cols = ['Year']
g = df.groupby(rows + columns)
s_av = g.apply(lambda x: np.average(x['snittpris'], weights=x['Antal_forsandelser']))
df_av = s_av.unstack(cols)

Posted originally on Stackoverflow.

Another more knowledgeable user has verified the problem.


Reproducible example

df = pd.DataFrame([[1] * 5], columns=['Konskund_MEAB','ProdID', 'Year', 'snittpris', 'Antal_forsandelser'])
rows = ['Konskund_MEAB','ProdID']
cols = ['Year']
aggfunc = lambda x: np.average(x['snittpris'], weights=x['Antal_forsandelser'])

df.pivot_table(index=rows, columns=cols, aggfunc=aggfunc)
# KeyError: 'snittpris'

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions