I have the following pandas DataFrame named table
grh pm_0 age_0
0 1 39054414 74
1 2 34054409 37
2 3 3715955000 65
3 4 19373605 53
4 5 99411 64
5 6 25664143 37
6 7 5161112 77
7 8 41517547 80
8 9 9517054000 72
9 10 538129400 52
I have a loop iterating over like this :
df2=df.copy()
for k in range (1,3):
for i in range (1,5):
df["pm_"+str(i)]=df["pm_"+str(i-1)]/k
df2=df2.append(df)
print(df2.head(15))
It works but i would like to encapsule it in a function. I tried something like this but it doesn't work. I think i made something wrong..
def sto(scn):
df4=df.copy()
for k in range (1,scn):
for i in range (1,5):
df["pm_"+str(i)]=df["pm_"+str(i-1)]/k
df4=df4.append(df)
sto(3)
print(df4)
Traceback (most recent call last): File "", line 11, in print(df4) NameError: name 'df4' is not defined
Any idea ?