Create a new field within a table and enter the value of another but taking the value of a next row

0

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

    
asked by Charoeci1 24.01.2017 в 21:02
source

1 answer

1

I have already solved my problem so here I put the final result in case someone is interested. This the code that I have at the end:

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
    ord_df.loc[idx, 'Cierre1'] = np.nan
    print(ord_df)

Cierre1(df,NumIntervalo)

Here I put an image with the result as I wanted it. If you copy the code in your program and modify the variable NumIntervalo you will see that it works well with other numbers that you put.

Greetings to all.

Charo.

    
answered by 25.01.2017 в 12:29