Solution for SpecificationError: nested renamer is not supported while agg() along with groupby()

前端 未结 10 2208
有刺的猬
有刺的猬 2020-12-15 21:23
def stack_plot(data, xtick, col2=\'project_is_approved\', col3=\'total\'):
    ind = np.arange(data.shape[0])

    plt.figure(figsize=(20,5))
    p1 = plt.bar(ind, d         


        
10条回答
  •  粉色の甜心
    2020-12-15 22:01

    Not a very elegant solution but this one works. As renaming the column is deprecated with the way you are doing. But there is work around. Create a temporary variable 'approved' , store the col2 in it. Because when you apply agg function , the original column values will change with column name. You can preserve the column name but then values in those column will change. So in order to preserve the original dataframe and to have two new columns with desired names, you can use the following code.

    approved = temp[col2]
    temp = pd.DataFrame(project_data.groupby(col1)[col2].agg([('Avg','mean'),('total','count')]).reset_index())
    temp[col2] = approved
    

    P.S : Seems like an assignment of AAIC, I am working on same :)

提交回复
热议问题