简体   繁体   中英

Script in python/pandas works but doesn't work when placed in side a function

I have this script I'm running to try to create a dataframe to summarize some statistics:

month = [may,june,july,august,sept]
month_str = [5,6,7,8,9]
avg_age = []
avg_use = []
avg_kwh = []
avg_coll = []
avg_cred = []
for i in month:
    avg_age.append(i[i['Age']!=0]['Age'].mean())
    avg_use.append(i[i['AverageBilledUsage']!=0]['AverageBilledUsage'].mean())
    avg_kwh.append(i[i['AverageKWH']!=0]['AverageKWH'].mean())
    avg_coll.append(i[i['Total Collected']!=0]['Total Collected'].mean())
    avg_cred.append(i[(i['credit_score']!=0) & (i['credit_score']!=99999)]['credit_score'].mean())
pd.DataFrame(data = [avg_age,avg_use,avg_kwh,avg_coll,avg_cred],columns = month_str,index = ['Age','Usage','kwh','collected','creditscore'])

It returns exactly what I want to see. But when I place it inside a function I get the following error:

AssertionError: 5 columns passed, passed data had 1 columns

Here is the code inside the function:

def get_nums():
    months = [may,june,july,august,sept]
    month_str = [5,6,7,8,9]
    avg_age = []
    avg_use = []
    avg_kwh = []
    avg_coll = []
    avg_cred = []
    for i in months:
        avg_age.append(i[i['Age']!=0]['Age'].mean())
        avg_use.append(i[i['AverageBilledUsage']!=0]['AverageBilledUsage'].mean())
        avg_kwh.append(i[i['AverageKWH']!=0]['AverageKWH'].mean())
        avg_coll.append(i[i['Total Collected']!=0]['Total Collected'].mean())
        avg_cred.append(i[(i['credit_score']!=0) & (i['credit_score']!=99999)]['credit_score'].mean())
        this_df = pd.DataFrame(data = [avg_age,avg_use,avg_kwh,avg_coll,avg_cred],columns = month_str,index = ['Age','Usage','kwh','collected','creditscore'])
    return this_df

You have a problem with the last line of the for loop in the function. this_df is being defined in every iteration of the loop.

The corrected code is below.

def get_nums():
    months = [may,june,july,august,sept]
    month_str = [5,6,7,8,9]
    avg_age = []
    avg_use = []
    avg_kwh = []
    avg_coll = []
    avg_cred = []
    for i in months:
        avg_age.append(i[i['Age']!=0]['Age'].mean())
        avg_use.append(i[i['AverageBilledUsage']!=0]['AverageBilledUsage'].mean())
        avg_kwh.append(i[i['AverageKWH']!=0]['AverageKWH'].mean())
        avg_coll.append(i[i['Total Collected']!=0]['Total Collected'].mean())
        avg_cred.append(i[(i['credit_score']!=0) & (i['credit_score']!=99999)]['credit_score'].mean())
    this_df = pd.DataFrame(data = [avg_age,avg_use,avg_kwh,avg_coll,avg_cred],columns = month_str,index = ['Age','Usage','kwh','collected','creditscore'])
    return this_df

Base on my understanding , you do not need the for loop here

month = [may,june,july,august,sept]
month_str = [5,6,7,8,9]
df=pd.concat(month,keys=month_str)

df=df.mask(df==0|df==99999)

df.groupby(level=0).mean().T

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