Home > Net >  Python Pandas - Starts with and replace
Python Pandas - Starts with and replace

Time:09-07

I have a column that contains numbers and strings. I want to find the cell that starts with '0' and replace it with Unknown

column1
12 - abcsffj
40 - dhjsadjal
0 - dahdalk
70 - dkalskda

I tried using .loc and contains but it also changed the cells that had 70 and 40 values.

df.loc[df['column1'].str.contains('0'), 'column1'] = 'Unknown'

Expected output

column1
12 - abcsffj
40 - dhjsadjal
Unknown
70 - dkalskda

CodePudding user response:

You need to use str.startswith rather than str.contains because contains will return True regardless the position of 0 in the values, whereas startswith will return True only if the values start with 0

>>> df.loc[df['column1'].str.startswith('0'), 'column1'] = 'Unknown'

          column1
0    12 - abcsffj
1  40 - dhjsadjal
2         Unknown
3   70 - dkalskda
  • Related