abhi1610
abhi1610

Reputation: 743

Merge row entries based on data in columns pandas

I have a dataframe as follows:

     Company    LT    MT    ST
0    XYZ        A     -     -
1    XYZ        A     -     B
2    XYZ        -     C     B
3    ABC        R     -     -
4    ABC        R     -     B
5    DEF        A     B     -
6    DEF        A     B     B

what I want is to make a dataframe that can check whether company is duplicated or not and based on the duplication check for muliple columns LT, MT & ST. And if the values in these columns are repeatative than merge it onto single row entry for same company name. Output as follows:

    Company    LT    MT    ST
0   XYZ        A     C     B
1   ABC        R     -     B
2   DEF        A     B     B

I have tried with df.drop_duplicates() but it doesn't solve my problem.

Upvotes: 3

Views: 42

Answers (1)

Zero
Zero

Reputation: 76917

Use groupby and first with fillna

In [559]: (df.replace('-', np.nan)
             .groupby('Company', sort=False, as_index=False)
             .first()
             .fillna('-'))
Out[559]:
  Company LT MT ST
0     XYZ  A  C  B
1     ABC  R  -  B
2     DEF  A  B  B

Upvotes: 4

Related Questions