Reputation: 161
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
Reputation: 15240
You should
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
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