[英]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我有一个 dataframe 与 Column[ID] 和值 ('123456','102554','0145220','1554201','0155401','0101010110','0101010105') 如果最后 2 个字符的列开始然后更新没有那个 0 的值
Final result of dataframe should be ('123456','102554','0145220','155421','015541','0101010110','010101015') dataframe 的最终结果应该是 ('123456','102554','0145220','155421','015541','0101010110','010101015')
Please help请帮忙
You can leverage pd.Series.replace(..., regex=True)
:您可以利用
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.