0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Perf! Thanks buddy.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.