Home > Mobile >  Nested for loop pandas dataframe does not create a new column
Nested for loop pandas dataframe does not create a new column

Time:12-24

I have this nested loop:

rolling=['10','20']

for i in range(len(ex)):
    for d in rolling:
       df = nested_df.loc[other_nested_df['pio'][i]:(other_nested_df['pio'][i] 10)].copy()
       ex.loc[i]['date_vert_' str(d)=df['date'].iloc[-1]

The loop does not give any error but I cannot understand why at the end the dataframe ex does not have other two columns called date_vert_10 and date_vert_20

CodePudding user response:

Change ex.loc[i][...] to ex.loc[i, ...]:

rolling=['10','20']

for i in range(len(ex)):
    for d in rolling:
       df = nested_df.loc[other_nested_df['pio'][i]:(other_nested_df['pio'][i] 10)].copy()
       ex.loc[i, 'date_vert_' str(d)=df['date'].iloc[-1]
       # ^^^^^^^^^^^ changed
  • Related