I commented: I have previously asked a question "Create a new field within a table and enter the value of another but taking the value of a previous row" and I have already solved. Now another problem arises and I want to do the same but taking the value of the next row. Here I put the code used in my previous question to see if you could tell me how to take the values of the next row.
import pyodbc
import pandas as pd
import numpy as np
df = pd.DataFrame({'IdActivo': [1,2,3,3,2,1,1,3,2],
'Fecha' : ['2009-01-01','2009-02-01','2009-02-01','2009-03-01','2009-03-01','2009-03-01','2009-04-01','2009-04-01','2009-04-01'],
'Cierre' : [25.5,26.04,88.8,26.8,24.8,27.5,23.05,27.8,30.20]})
NumIntervalo =2 # Me he creado una variable para poner el número de intervalo que quiero mostrar. Así sólo con cambiar el valor aquí no tendré que modificar el procedimiento.
def Cierre1(df,NumIntervalo):
ord_df = df.sort_values(by=['IdActivo', 'Fecha'])
ord_df ['Cierre1'] = ord_df['Cierre'].shift(NumIntervalo)
idx = ord_df.iloc[NumIntervalo:]['IdActivo'].values != ord_df.iloc[:len(ord_df) - NumIntervalo]['IdActivo'].values
for i in range(NumIntervalo):
idx = np.append([False], idx)
ord_df.loc[idx, 'Cierre1'] = np.nan
print(ord_df)
Cierre1(df,NumIntervalo)
I have tried to put the value NumIntervalo in negative within the procedure but I do not get the results I want. I put an image of how I would like the data to appear correctly.
Waiting for your help I say goodbye attentively.
Charo