Reputation: 2273
I have this df ( in the real case I have more duplicated rows):
xx yy zz name
A 5 6 0 A
A 0 0 -1 A
C 6 7 5 C
How could I merge the 2 rows those indices are the same and keeping only the values different from zero among them? The desired output would be :
xx yy zz name
A 5 6 -1 A
C 6 7 5 C
Upvotes: 0
Views: 38
Reputation: 25249
you may try df.where
and groupby first
df.where(df.ne(0)).groupby(level=0).first()
Out[161]:
xx yy zz name
A 5.0 6.0 -1.0 A
C 6.0 7.0 5.0 C
Upvotes: 2
Reputation: 42916
You can also use replace
, bfill
, query
and duplicated
:
df.replace(0, np.NaN).bfill().query('~index.duplicated()')
Output
xx yy zz name
A 5.0 6.0 -1.0 A
C 6.0 7.0 5.0 C
Upvotes: 1
Reputation: 76297
Try
df.replace({0: None}).groupby(df.index).max()
xx yy zz name
A 5 6 7 A
C 6 7 5 C
It first replaces the 0 elements by None
, which is ignored by max
in the groupby
.
Upvotes: 1