Audrius Šaučiūnas
Audrius Šaučiūnas

Reputation: 328

How do I apply a self made function on a dataframe column to fill NaN values?

I have a dataset with rows like this: enter image description here

I would like to apply my function to each row where air_time is NaN so that I can set the air_time depending on what the departure and arrival time is. How would I go around this?

def get_air_time(dep_time, arr_time):
    converted_dep_time = datetime.timedelta(minutes=(dep_time // 100) * 60 + (dep_time % 100))
    converted_arr_time = datetime.timedelta(minutes=(arr_time // 100) * 60 + (arr_time % 100))
    delta = converted_arr_time - converted_dep_time 
    return abs(delta.total_seconds() / 60)

Upvotes: 0

Views: 49

Answers (1)

prahasanam_boi
prahasanam_boi

Reputation: 896

Please try this:

df['air_time'] = df[['dep_time', 'arr_time']].apply(get_air_time, axis=1)

Upvotes: 1

Related Questions