exception
exception

Reputation: 21

Python - Creating an array for a series within a loop?

I'd like to add values calculated in a for loop to a series so that it can be its own column in a dataframe. So far I've got this: the y values are from a dataframe named block.

N = 12250
for i in range(0,N-1):
    y1 = block.iloc[i]['y']
    y2 = block.iloc[i+1]['y']
    diffy[i] = y2-y1

I'd like to make diffy its own series instead of just replacing the diffy val on each loop

Upvotes: 2

Views: 128

Answers (1)

Code Different
Code Different

Reputation: 93181

Some sample data (assume N = 5):

N = 5

np.random.seed(42)
block = pd.DataFrame({
    'y': np.random.randint(0, 10, N)
})

   y
0  6
1  3
2  7
3  4
4  6

You can calculate diffy as follow:

diffy = block['y'].diff().shift(-1)[:-1]

0   -3.0
1    4.0
2   -3.0
3    2.0
Name: y, dtype: float64

diffy is a pandas.Series. If you want list, add .to_list(). If you want a numpy array, add .values

Upvotes: 1

Related Questions