Search code examples
pythonpandasdataframecoalesce

Coalesce Pandas DataFrame DOWN Columns


I've seen lots of solutions for coalescing columns together (left to right, etc) But I need to do it row-wise.

Col A Col B Col C Col D Col E
Row 1 NA NA 1 text
Row 2 NA NA NA text
Row 3 NA 4 10 NA
Row 4 20 5 NA text

I want my result to be a single row that looks like...

Col A Col B Col C Col D Col E
Row 1 20 4 1 text

I could iterate over each column, find the first non-NA entry.. then use that as the value. However, I need to do this for hundreds of thousands of distinct partitioned tables for ~30 columns. Surely there's a better solution!

Any help is appreciated :)


Solution

  • Try:

    print(df.bfill().head(1))
    

    Prints:

       Col A  Col B  Col C  Col D Col E
    0  Row 1   20.0    4.0    1.0  text