The DataFrame:
import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': [2, 6, 7, 8, 2]})
col1 | |
---|---|
0 | 2 |
1 | 6 |
2 | 7 |
3 | 8 |
4 | 2 |
A function to create a new column being a cumulative sum (I'm aware of cumsum()
, this was just a simple test before doing more complex functions):
def funcadd(inputarr):
for i in range(1, len(inputarr)):
inputarr[i] = inputarr[i] + inputarr[i-1]
return inputarr
df['new'] = funcadd(df['col1'].values)
col1 | new | |
---|---|---|
0 | 2 | 2 |
1 | 8 | 8 |
2 | 15 | 15 |
3 | 23 | 23 |
4 | 25 | 25 |
As you can see, for some reason col1
gets modified despite never issuing a command to change it (?)
I've tried:
- immediately doing
arr1 = inputarr
in the function then only usingarr1
in the rest of the function - doing
arr2 = df['col1'].values
before calling the function witharr2
- swapping
.values
with.to_numpy