简体   繁体   English

python / pandas中的脚本有效,但放在函数旁边时不起作用

[英]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. 函数中for循环的最后一行存在问题。 this_df is being defined in every iteration of the loop. 在循环的每次迭代中都定义了this_df。

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 根据我的理解,这里不需要for循环

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas:与apply()一起使用时功能不起作用 - Python Pandas: Function doesn't work when used with apply() 在python脚本文件中使用时,type()函数不起作用 - type() function doesn't work when used in a python script file Python 3 cgi脚本不起作用,但是python 2有效 - Python 3 cgi script doesn't work, but python 2 works Python 脚本在 Sublime Text 中运行时不起作用,但在命令行中运行 - Python script doesn't work when run in Sublime Text, but works from command line 通过终端运行时,Python脚本无法正常运行,但在Jupyter和Visual Studio中可以正常运行 - Python script doesn't work properly when ran through terminal, but works fine in Jupyter and Visual Studio Python脚本在Windows上不起作用(但适用于mac) - Python script doesn't work on Windows (but works on mac) Python导入在解释器中起作用,在脚本Numpy / Matplotlib中不起作用 - Python import works in interpreter, doesn't work in script Numpy/Matplotlib `import pyspark`可在Jupyter中使用,但不适用于python shell / script - `import pyspark` works in Jupyter, but doesn't work with python shell/script 具有open('filename')的Python脚本可用于IDLE,但不能在控制台中使用 - Python script with open('filename') works with IDLE but doesn't work in console Python代码在函数外部起作用,但在函数内部不起作用 - Python code works outside function, but doesn't work inside function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM