名称错误:尝试调用函数时未定义名称“loan_tape”

2024-04-28 23:48:09 发布

您现在位置:Python中文网/ 问答频道 /正文

当我尝试执行函数时,收到一个NameError(未定义名称“loan tape”)。 函数的定义方式有什么问题?在

def create_table(table_name, num_rows, num_cols):
        table_name = pd.DataFrame(np.random.rand(num_rows,num_cols), columns = [["Exposure","EIR"]])
        return print(table_name.head(20))

(create_table(loan_tape ,20 ,2)

Tags: 函数name名称定义defcreate方式table
1条回答
网友
1楼 · 发布于 2024-04-28 23:48:09

如果你在做我认为的事情——那就是试图提前创建一个变量并将其初始化为一个数据帧,那么这绝对不是你要做的。在

传递2个参数,因为这就是您所需要的,然后return table。在

def create_table(num_rows, num_cols):
    table = pd.DataFrame(np.random.rand(num_rows,num_cols), 
                       columns=[["Exposure","EIR"]] 
           )

    return table.head(20)

loan_tape = create_table(20, 2)
print(loan_tape)

    Exposure       EIR
0   0.969132  0.379487
1   0.695092  0.787540
2   0.168266  0.989034
3   0.867826  0.499139
4   0.447891  0.922618
5   0.970134  0.252184
6   0.971446  0.049291
7   0.289744  0.797935
8   0.460266  0.176311
9   0.927201  0.280241
10  0.671764  0.520443
11  0.196516  0.258724
12  0.391544  0.190949
13  0.742233  0.590536
14  0.092953  0.558999
15  0.573201  0.505211
16  0.933630  0.656285
17  0.327771  0.264572
18  0.279868  0.527335
19  0.096123  0.560708

注意,不能返回print(...),因为print返回{}。在


编辑:将columns作为参数传递:

^{pr2}$

相关问题 更多 >