Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Please try this:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading