Ms.J
Ms.J

Reputation: 161

Combine rows and fill NaN values within groups

I have the following DataFrame:

    X   Y   Z   
0   xxx NaN 333 
1   NaN yyy 444     
2   xxx NaN 333     
3   NaN yyy 444 

I am trying to combine rows based on the Z column to get the following:

    X   Y   Z   
0   xxx yyy 333 
1   xxx yyy 444 

Upvotes: 1

Views: 405

Answers (2)

Igor Raush
Igor Raush

Reputation: 15240

You should

  1. group by the label column, and
  2. within each group, backfill NaN values, and return the first row.

For example,

def flatten(g):
    return g.fillna(method='bfill').iloc[0]

df.groupby('Z').apply(flatten).reset_index(drop=True)

Upvotes: 1

Stefan
Stefan

Reputation: 42905

For this particular example, you can do:

df.fillna(method='ffill').fillna(method='bfill').drop_duplicates()

     X    Y    Z
0  xxx  yyy  333
1  xxx  yyy  444

Not sure if you are looking for something more general?

Upvotes: 2

Related Questions