FungusAmungus
FungusAmungus

Reputation: 77

Collapsing rows in a Dataframe into one specific row to fill missing values?

I want to collapse values in all rows into the first (or specified) row

The dataframe looks like this:

id     column1     column2     column3
A                  15          
B      13  
C                              10

I want it to look like this:

id     column1     column2     column3
A      13            15        10  

Apologies if my formatting sucks, this is my first submission.

Upvotes: 2

Views: 190

Answers (1)

rafaelc
rafaelc

Reputation: 59284

A trick is to use GroupBy.first which by default ignores NA values.

df[df.ne('')].groupby(np.zeros(len(df))).first()

Another option is to use ''.join in each column, given you have empty strings in those empty slots.

df.astype(str).apply(''.join).to_frame().T

    id  column1  column2  column3
0.0  A     13.0     15.0     10.0

Upvotes: 2

Related Questions