Skip to content
Advertisement

Pandas – merge multiple time columns and fill values

I have a dataframe (df):

JavaScript

I need to merge the time columns, sort them, then fill the missing values in a “downwards” manner. The expected output is like so:

JavaScript

Advertisement

Answer

Use wide_to_long with some preprocessing – rename time column and convert index to column, then sorting by DataFrame.sort_values, remove dupes by DataFrame.drop_duplicates and missing values by DataFrame.dropna:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement