0

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 ?

Bebio
  • 409
  • 5
  • 12

2 Answers2

0

You just need to explicitly mark it as global object:

df4 = df.copy()
def sto(scn):
    global df4
    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)
    return df4

sto(3)

You can pass a dataframe to the function, do:

def sto(scn, df):
    dfx = df.copy()
    for k in range (1,scn):
        for i in range (1,5):
            df["pm_"+str(i)]=df["pm_"+str(i-1)]/k
        dfx=dfx.append(df)
    return dfx

df4 = sto(3, df)
YOLO
  • 20,181
  • 5
  • 20
  • 40
  • Ok, thks. But what if I need to generate a new dataframe in the loop. For instance, by adding 'df5=df4.copy()'. Is there a way to do that ? – Bebio Jul 31 '20 at 09:33
0

You need to send the df to the function and return it at the end:

def sto(scn, df)
.....
.....
     return df
gtomer
  • 5,643
  • 1
  • 10
  • 21