I receive a NameError (name 'loan tape' is not defined) when I try to execute the function. What is wrong with the way the function is defined?
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)
If you're doing what I think - and that is trying to create a variable in advance and initialise it as a dataframe, then this is definitely not how you do it.
Pass 2 arguments, because that's all you need, and then 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
Note that you must not return print(...)
because print
returns None
.
Edit: Passing columns
as an argument:
def create_table(num_rows, num_cols, use_cols):
table = pd.DataFrame(np.random.rand(num_rows,num_cols),
columns=use_cols)
return table.head(20)
loan_tape = create_table(20, 2, [["Exposure","EIR"]] )