admin管理员组

文章数量:1244316

I've made the following column:

df_base['CustomerDaysInterval'] = pd.cut(x=df_base['CustomerDays'], 
                                         bins=[0,15,30,45,60,75,90], 
                                         labels= ["0 to 15 days","15 to 30 days", "30 to 45 days", "45 to 60 days","60 to 75 days","75 to 90 days"])

After that, i'm having an issue when adding parameters margins=True:

df_pivot = pd.pivot_table(
            data=df_base.reset_index(),
            index= 'Sales Team',
            columns='CustomerDaysInterval',
            aggfunc='size',
            observed=True,
            margins=True).reset_index()

With the above code I get a KeyError: '0 to 15 days'. It should be noted that by removing the margins=True parameter, the code works properly.

本文标签: pythonpdpivot table issue when adding marginsTrueStack Overflow