0

How do I find an exact match of a string in a dataframe column?

Currently, I am searching col_8 for the string '1' and it is returning True for row #2, but I want it to be false because it is actually 12. Thanks in advance!

df['new'] = np.where(df['col_8'].str.contains('1'),1,0)
print(df)

    col_8  col_9  new
0    1,2,3  7     1
1    1,3,8  10    1
2    12     8     1
3    9      3     0
Shawn Schreier
  • 780
  • 2
  • 10
  • 20

1 Answers1

0

This would work

df['new'] = np.where(df['col_8'].str.contains('1,'),1,0)
Furqan Hashim
  • 1,304
  • 2
  • 15
  • 35