简体   繁体   中英

Why is it not possible to access other variables from inside the apply function in Python?

Why would the following code not affect the Output DataFrame? (This example is not interesting in itself - it is a convoluted way of 'copying' a DataFrame.)

def getRow(row):
     Output.append(row)

Output = pd.DataFrame()
Input = pd.read_csv('Input.csv')
Input.apply(getRow)

Is there a way of obtaining such a functionality that is using the apply function so that it affects other variables?

What happens

DataFrame.append() returns a new dataframe. It does not modify Output but rather creates a new one every time.

  DataFrame.append(self, other, ignore_index=False, verify_integrity=False) 

Append rows of other to the end of this frame, returning a new object. Columns not in this frame are added as new columns.

Here:

Output.append(row)

you create a new dataframe but throw it away immediately.

You have access - But you shouldn't use it in this way

While this works, I strongly recommend against using global :

df = DataFrame([1, 2, 3])
df2 = DataFrame()

def get_row(row):
    global df2
    df2 = df2.append(row)

df.apply(get_row)
print(df2)

Output:

   0  1  2
0  1  2  3

Take it as demonstration what happens. Don't use it in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM