简体   繁体   English

Python:从 dataframe 列值中删除特定字符

[英]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.

相关问题 如何提取特定编号来自 dataframe 列 Python 的字符 - How to extract specific no. of character from dataframe column Python Python:在数据框中用相同的值填充特定列并删除无用的行 - Python : Fill a specific column with the same value in a Dataframe and remove the rows useless 删除熊猫数据框中特定字符后的值 - Remove value after specific character in pandas dataframe 从列表 python 的每个值中删除特定字符 - remove a specific character from each value of a list python 列表的Pandas DataFrame列:删除特定值 - Pandas DataFrame Column of Lists: Remove a Specific Value 如果一列与值匹配,则从 dataframe 中删除行 - Python 3.6 - Remove rows from dataframe if one column matches a value - Python 3.6 Python:根据另一列值从 DataFrame 中删除重复项 - Python: Remove duplicates from DataFrame based on another column value Python 删除 dataframe 列中的特定字符串 - Python Remove specific string in dataframe column 在 pandas DataFrame 中,如何将列中的特定值存储到变量中,然后从列中删除该值? - In pandas DataFrame, how can I store a specific value from a column into a variable, and then subsequently remove that value from the column? 从 dataframe 列获取最相似的值到特定字符串 python - Get most similar value from dataframe column to specific string python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM