Python Forum

Full Version: Interate for loop over certain columns in dataframe
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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]-1
       
for 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.
First loop changes the dataframe and you have another set of columns when calling df.columns in line #11.

To fix this, just store column names in a variable, before changing the df, e.g.
col_to_process = df.columns

for tickers in col_to_process:
    ... your code (_future)

for tickers in col_to_process:
    ... your code (_lagged)
Thank you very much!