Skip to content
Advertisement

Pandas combine rows in groups to get rid of Nans

I want to do something similar to what pd.combine_first() does, but as a row-wise operation performed on a shared index. And to also add a new column in place of the old ones – while keeping the original_values of shared column names.

In this case the ‘ts’ column is one that I want to replace with time_now.

JavaScript

My desired output is below, using the new timestamp, but keeping the old ones based on their group index.

JavaScript

The part below seems to work when run by itself. But I cannot get it to work inside of a function because of differences between index lengths.

JavaScript

Advertisement

Answer

The problem was that I forgot to put the dictionary into a list to create a records oriented dataframe. Additionally when using a similar function, the index might need to be dropped to be reset, as duplicated columns might be created.

I still wonder if there’s a better way to do what I want, since it’s kind of slow.

JavaScript
Advertisement