简体   繁体   中英

Python Pandas - Starts with and replace

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM