小编Bob*_*y M的帖子

python:从用户定义的函数返回 pandas 数据帧时遇到问题(可能是用户错误)

我有一个创建 DataFrame 的函数。在该功能中我可以将其打印出来。但我在返回过程中做错了,因为运行函数后我似乎无法调用 DataFrame。下面是我的虚拟代码和附加的错误。

import pandas as pd
def testfunction(new_df_to_output):
    new_df_to_output = pd.DataFrame()
    S1 = pd.Series([33,66], index=['a', 'b'])
    S2 = pd.Series([22,44], index=['a', 'b'])
    S3 = pd.Series([11,55], index=['a', 'b'])

    new_df_to_output = new_df_to_output.append([S1, S2, S3], ignore_index=True)
    print new_df_to_output
    print type(new_df_to_output)
    print dir()
    return new_df_to_output

testfunction('Desired_DF_name')

print dir()
print Desired_DF_name
Run Code Online (Sandbox Code Playgroud)

DataFrame 在函数内正确打印。目录显示该函数之后没有返回DataFrame。尝试打印该数据帧返回返回以下错误

回溯(最近一次调用最后一次):文件“functiontest.py”,第 21 行,打印 Desired_DF_name NameError:名称“Desired_DF_name”未定义

我确信这是一个简单的错误,但在搜索 Stackoverflow 和 python 教程后我找不到解决方案。非常感谢任何指导。

python function

4
推荐指数
1
解决办法
1万
查看次数

如何使用具有时间相关变量的scipy.integrate.odeint求解ODE系统

我正在使用scipy食谱中的Zombie Apocalypse 示例,以了解有关使用python解决ODE系统的信息。

在此模型中,有一个方程式可以根据出生率,死亡率和初始人口提供每天的人口。然后根据人口数量,计算出有多少僵尸被制造和杀死。

我有兴趣用一系列可以告诉我们每个时间步长人口的数据列表来代替人口微分方程。我收到以下错误:

TypeError: can't multiply sequence by non-int of type 'float'
Run Code Online (Sandbox Code Playgroud)

正如人们指出的那样,这是因为将单个数字乘以列表没有意义。我不确定如何在每次T时从列表中为微分方程提供数字。

这是两次尝试的代码

# solve the system dy/dt = f(y, t)
def f(y, t):
    Si = [345, 299, 933, 444, 265, 322] # replaced an equation with list
    Zi = y[0]
    Ri = y[1]
    # the model equations (see Munz et al. 2009)
    f0 = B*Si*Zi + G*Ri - A*Si*Zi
    f1 = d*Si + A*Si*Zi - G*Ri
    return [f0, f1]
Run Code Online (Sandbox Code Playgroud)

我也尝试过

numbers = [345, 299, 933, …
Run Code Online (Sandbox Code Playgroud)

python scipy ode

3
推荐指数
1
解决办法
2206
查看次数

标签 统计

python ×2

function ×1

ode ×1

scipy ×1