MaskedMonkey
MaskedMonkey

Reputation: 117

merging 2 partially matching rows python

I am currently trying to construct a dataframe from multiple sources using pandas. I am having the issue that I am unable to merge two rows within a dataframe that are partially matching. The example:

input:

|String  |  A  |  B  |  C  |
|--------------------------|
|Hey     |  1  | NaN |  2  |
|Bye     |  1  |  2  |  3  |
|Hey     | NaN |  5  | NaN |

wanted output:

|String  |  A  |  B  |  C  |
|--------------------------|
|Hey     |  1  |  5  |  2  |
|Bye     |  1  |  2  |  3  |

Any Help would be greatly appreciated.

Upvotes: 1

Views: 45

Answers (1)

jezrael
jezrael

Reputation: 863341

IIUC need GroupBy.first:

df = df.groupby('String', sort=False, as_index=False).first()
print (df)
  String    A    B    C
0    Hey  1.0  5.0  2.0
1    Bye  1.0  2.0  3.0

Upvotes: 1

Related Questions