简体   繁体   中英

pandas pivot_table returns empty dataframe

I get an empty dataframe when I try to group values using the pivot_table. Let's first create some stupid data:

import pandas as pd
df = pd.DataFrame({"size":['large','middle','xsmall','large','middle','small'],
                   "color":['blue','blue','red','black','red','red']})

When I use:

df1 = df.pivot_table(index='size', aggfunc='count')

returns me what I expect. Now I would like to have a complete pivot table with the color as column:

df2 = df.pivot_table(index='size', aggfunc='count',columns='color')

But this results in an empty dataframe. Why? How can I get a simple pivot table which counts me the number of combinations? Thank you.

you need another column to be used as values for aggregation.

Add a column -

df['freq']=1

Your code will work.

You need to use len as the aggfunc, like so

df.pivot_table(index='size', aggfunc=len, columns='color')

If you want to use count, here are the steps:

  1. First add a frequency columns, like so:

     df['freq'] = df.groupby(['color', 'size'])['color'].transform('count') 
  2. Then create the pivot table using the frequency column:

     df.pivot_table(values='freq', index='size', aggfunc='count', columns='color') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM