最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - pd.pivot table issue when adding margins=True - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论