Home > Blockchain >  Alternative of pd.NamedAgg to a code compliant with pandas 0.24.2?
Alternative of pd.NamedAgg to a code compliant with pandas 0.24.2?

Time:08-27

When I'm trying to run my code, I'm facing the error: AttributeError: module 'pandas' has no attribute 'NamedAgg' . Can someone help me with the alternative of this?. Thanks My code:

import pandas as pd
import re

df = pd.read_excel('testingfile.xlsx')
df_grouped = df.groupby(['result_by', 'variable']).agg(count_col=pd.NamedAgg(column='variable', aggfunc="count"))
df1=df_grouped.reset_index()
subsetDataFrame = df1[df1['result_by'].isin(['mango', 'apple','guava','berry']) ]
final_result=subsetDataFrame.pivot(index='variable',columns='result_by' ,values='count_col').fillna('-')
final_result

CodePudding user response:

As explained in comment, you can do this:

df = pd.read_excel('testingfile.xlsx')
df_grouped = df.groupby(['result_by', 'variable']).agg({'variable':"count"}).rename(column={'variable':'count_col'})

The rest of the code can be the same.

Anyway your problem can be solved if you upgrade your pandas to newer (0.25 ) version.

  • Related