简体   繁体   中英

Pandas pivot_table not behaving as expected when having few different values in groupby.count

I want to make a pivot table after grouping and counting the number of uniqueid per day and per license. This is what I usually use and it produces the expected result :

import pandas as pd

raw_data = {'day' : ['Monday','Monday','Monday','Monday','Monday'], 
            'license':['A','A','C','B','B'],
            'uniqueid':[123,352,737,368,901]}
df = pd.DataFrame(raw_data).groupby(['day','license']).count()
pivot = df.pivot_table(index = 'day', columns = 'license', values = ['uniqueid'])
pivot

Result :

        uniqueid      
license        A  B  C
day                   
Monday         2  2  1

But if I change the 'C' license for a 'B' license in raw_data, I get the following :

         uniqueid
day             2
license         3

I would have liked to get the same result as before, minus the 'C' column.

The problem seems to be in the pivot_table function, because the groupby/count function returns me properly separated data. It also works if I have two or more types of day. Any advice? Thank you!

After tagging an issue on the Pandas GitHub, this is a bug that will be resolved in the next release. In the meantime, using df.unstack() produces the expected result.

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