2

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?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
tsotsi
  • 683
  • 2
  • 8
  • 20

1 Answers1

5

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.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
  • Thanks a lot. Just for the sake of completeness, why would you say it's a bad idea to use `global` that way? – tsotsi Jan 07 '16 at 22:36
  • 1
    When you use `global`, you are prone to create implicit dependencies between seemingly unrelated pieces code. This can lead to very hard to understand programs. A function that changes the "outside world" can create a lot of problems that can be hard to hunt down. – Mike Müller Jan 07 '16 at 22:41
  • 1
    Technically speaking, I think the variable is "outside world" in the case of calling a function through `.apply` is because the function is called internally from the pandas library rather from the script itself. Thus, the variable won't be seen if not `global`. – otayeby Jul 22 '17 at 18:34