Home > Software design >  How to fill a column on a dataframe based on given conditions using a for-loop
How to fill a column on a dataframe based on given conditions using a for-loop

Time:07-07

I have a pandas dataframe where the column ARRIVAL_DELAY is filled with different values (float64). I want to insert a new column COMPENSATION which will be filled depending on the values from ARRIVAL_DELAY.

I want it to look like this:

ARRIVAL_DELAY      COMPENSATION
      10                0
      25                0
      43                50
      61                250

I came up with the code below but it just seems that is not working at all (it is taking literally hours on Jupyter notebooks and it is not even completing anything). Also, no errors nor warnings are displayed:

fdf.insert(13, 'COMPENSATION', 0)

compensacion = [0, 50, 100, 250, 500, 1000]

for row in fdf['ARRIVAL_DELAY']:
    if row > 0 and row <= 15 : fdf['COMPENSATION'].add(compensacion[0])
    
    elif row > 15 and row <= 30 : fdf['COMPENSATION'].add(compensacion[1])
    
    elif row > 30 and row <= 60 : fdf['COMPENSATION'].add(compensacion[2])
    
    elif row > 60 and row <= 120 : fdf['COMPENSATION'].add(compensacion[3])
    
    elif row > 120 and row <= 180 : fdf['COMPENSATION'].add(compensacion[4])
    
    else :fdf['COMPENSATION'].add(compensacion[5])
          
fdf.head(10)

I do not understand what's wrong, any ideas?

Finally, I am kind of new to Python so if anyone has an improvement idea, it will be more than welcomed

  • Related