Sam
Sam

Reputation: 404

find common rows based on specific columns in a dataframe

I have a dataframe, I would like to find the common rows based on a specific columns.

packing_id  col1  col2  col3 col4
1            1.0  2.0
2            2.0  2.0
3            1.0  1.0
4            3.0  3.0
.             .    .
.             .    .

I would like to find the rows where the col1 and col2 values are the same. I tried np.where(df.col1==df.col2) but it is not the right approach. I would appreciate your advice. Thanks.

Upvotes: 0

Views: 148

Answers (2)

Sam
Sam

Reputation: 404

This should be work. Thanks

df[df.col1==df.col2]

Upvotes: 1

jezrael
jezrael

Reputation: 862521

I think your soluton is good, only add 2 parameters to np.where:

df['new'] = np.where(df.col1==df.col2, 'same', 'no same') 

If need filter them:

df1 = df[df.col1==df.col2]

Upvotes: 1

Related Questions