I would like to do the following: I have a table with several fields 'IdActive, Date, Close' and I want to add another new field 'Closure_1' which I want to introduce the values that are in the Closing field but taking the value before the record in which I am. Here I put an image of the data of my table and I have put the field that I would like to create with the resulting data to see if you could help me.
Here I put the code that I am using:
import pyodbc
import pandas as pd
import numpy as np
df = pd.DataFrame({'IdActivo': [1,2,3,3,2,1,3],
'Fecha' : ['2009-01-01','2009-02-01','2009-02-01','2009-03-01','2009-03-01','2009-03-01','2009-04-01'],
'Cierre' : [25.5,26.5,25.8,26.8,24.8,27.5,27.8]})
def Cierre1(df):
ord_df = df.sort_values(by=['IdActivo', 'Fecha'])
print(ord_df)
Cierre1(df)
I have sorted the table by the IdActive and Date fields.
Waiting for your answer I say goodbye to you
Charo
I have continued working on it and I have put the following:
def Cierre1(df):
ord_df = df.sort_values(by=['IdActivo', 'Fecha'])
ord_df ['Cierre1'] = ord_df.iloc[1:]['Cierre']
idx = ord_df.iloc[1:]['IdActivo'].values != ord_df.iloc[:len(ord_df) - 1]['IdActivo'].values
idx = np.append([True], idx)
ord_df.loc[idx, 'Cierre1'] = np.nan
print(ord_df)
Cierre1(df)