Python Forum
Interate for loop over certain columns in dataframe - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Interate for loop over certain columns in dataframe (/thread-24806.html)



Interate for loop over certain columns in dataframe - Finpyth - Mar-05-2020

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.


RE: Interate for loop over certain columns in dataframe - scidam - Mar-06-2020

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)



RE: Interate for loop over certain columns in dataframe - Finpyth - Mar-06-2020

Thank you very much!