Mar-05-2020, 12:17 PM
Hallo.
I am working on my bachelor thesis where i want to run a loop over certain columns in my dataframe and create future and lagged returns of a portfolio.
I works fine for the first loop(future) but in the second loop(lagged) it creates a tickers_future_lagged
Keep in mind i load the data from a CSV-file and want to do this for whole index of stocks and several periods of lagged returns.
the the second loop it creates 6 new colums, both a tickers_lagged and a tickers_future_lagged and i only want the tickers_lagged and not the tickers_future_lagged
How do i run the loop so i only create the right column?
Hope you understand my question.
Best regards, Finpyth from Denmark.
I am working on my bachelor thesis where i want to run a loop over certain columns in my dataframe and create future and lagged returns of a portfolio.
I works fine for the first loop(future) but in the second loop(lagged) it creates a tickers_future_lagged
Keep in mind i load the data from a CSV-file and want to do this for whole index of stocks and several periods of lagged returns.
import pandas as pd df_1 = pd.read_csv("data.csv") df_1 = df_1.set_index('Date') tickers = (['DANSKE.CO', 'NOVO.CO', 'ORSTED.CO']) for tickers in df.columns: if df[tickers].dtype == 'float': df[tickers+'_future'] = df[tickers].shift(-1)/df[tickers]-1 for tickers in df.columns: if df[tickers].dtype == 'float': df[tickers+'_lagged'] = df[tickers].shift(1)/df[tickers]-1for the first loop it creates 3 new colums with tickers_future that is correct
the the second loop it creates 6 new colums, both a tickers_lagged and a tickers_future_lagged and i only want the tickers_lagged and not the tickers_future_lagged
How do i run the loop so i only create the right column?
Hope you understand my question.
Best regards, Finpyth from Denmark.