0

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.

Sam
  • 352
  • 2
  • 4
  • 22
  • 1
    Does this answer your question? [Pandas conditional creation of a series/dataframe column](https://stackoverflow.com/questions/19913659/pandas-conditional-creation-of-a-series-dataframe-column) – Vishnudev Krishnadas Mar 24 '21 at 08:23

2 Answers2

1

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]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

This should be work. Thanks

df[df.col1==df.col2]
Sam
  • 352
  • 2
  • 4
  • 22