Tim
Tim

Reputation: 1

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

Upvotes: 0

Views: 39

Answers (1)

Georgina Skibinski
Georgina Skibinski

Reputation: 13377

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

Upvotes: 3

Related Questions