简体   繁体   中英

Python: Remove specific character from dataframe column value

I have a dataframe with Column[ID] and values ('123456','102554','0145220','1554201','0155401','0101010110','0101010105') If last 2 characters of the column starts with 0 then update the value without that 0

Final result of dataframe should be ('123456','102554','0145220','155421','015541','0101010110','010101015')

Please help

You can leverage pd.Series.replace(..., regex=True) :

df["ID"] = df["ID"].replace("^(.*)0(.)$", "\\1\\2", regex=True)

Outputs:

           ID
0      123456
1      102554
2     0145220
3      155421
4      015541
5  0101010110
6   010101015

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