Kaustubh Shrotri
Kaustubh Shrotri

Reputation: 13

Grouping corresponding Rows based on One column

I have an Excel Sheet Dataframe with no fixed number of rows and columns. eg.

Col1 Col2 Col3
A    1    -
A    -    2
B    3    -
B    -    4
C    5    -

I would like to Group Col1 which has the same content. Like the following.

Col1 Col2 Col3
A    1    2
B    3    4
C    5    -

I am using pandas GroupBy, but not getting what I wanted.

Upvotes: 1

Views: 37

Answers (1)

U13-Forward
U13-Forward

Reputation: 71580

Try using groupby:

print(df.replace('-', pd.np.nan).groupby('Col1', as_index=False).first().fillna('-'))

Output:

  Col1 Col2 Col3
0    A    1    2
1    B    3    4
2    C    5    -

Upvotes: 1

Related Questions