2

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

3
  • could you perhaps clarify your desired output? Commented Jan 3, 2020 at 14:42
  • so s=pd.Series(range(0,N-1)) and s.shift(-1)-s ? Commented Jan 3, 2020 at 14:42
  • What kind of object is diffy? A list? A dictionary? A pandas.Series? Commented Jan 3, 2020 at 14:44

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.