Home > Mobile >  For Loops to copy and edit DataFrames
For Loops to copy and edit DataFrames

Time:11-16

I want to create 24 dataframes in a Loop, where the first dataframe is created by:

df_hour_0 = df_dummies.copy()
df_hour_0['Price_24_1'] = df_hour_0['Price_REG1']
df_hour_0['Price_24_2'] = df_hour_0['Price_REG2']
df_hour_0['Price_24_3'] = df_hour_0['Price_REG3']
df_hour_0['Price_24_4'] = df_hour_0['Price_REG4']

df_hour_0[['Price_24_1', 'Price_24_2', 'Price_24_3', 'Price_24_4'
           ]] = df_hour_0[['Price_24_1', 'Price_24_2', 'Price_24_3', 'Price_24_4']].shift(1)

First, I tried this approach, which doesn't work. (Maybe another version of this approach can work?).


for i in range(24):
    df_hour_i = df_dummies.copy()
    df_hour_i['Price_24_1'] = df_hour_i['Price_REG1']
    df_hour_i['Price_24_2'] = df_hour_i['Price_REG2']
    df_hour_i['Price_24_3'] = df_hour_i['Price_REG3']
    df_hour_i['Price_24_4'] = df_hour_i['Price_REG4']
    df_hour_i[['Price_24_1', 'Price_24_2', 'Price_24_3', 'Price_24_4']] = df_hour_0[['Price_24_1', 'Price_24_2', 'Price_24_3', 'Price_24_4']].shift(i 1)
    

Now I have read that people, in general, recommend using a dictionary so I have tried this:

d = {}
for x in range(23):
    d[x] = pd.DataFrame()

which gives me 24 empty DF in one dictionary, but now I struggle with how to fill them.

CodePudding user response:

Build a list of DataFrames like this:

cols =  ['Price_24_1', 'Price_24_2', 'Price_24_3', 'Price_24_4']
dfs = []

for i in range(24):
    df = df_hour_0.copy()
    df[cols] = df[cols].shift(i)
    dfs.append(df)

Shift by i will be at index i.

  • Related