gpt4 book ai didi

python - 如何在 Pandas 中删除左右字符串中的所有 '0'

转载 作者:太空宇宙 更新时间:2023-11-04 09:27:36 25 4
gpt4 key购买 nike

我想删除 pandas 数据框中左右字符串中的所有“0”。

这是我的数据集:

Id    Label
0 000010
1 001010
2 000111
3 111100

这是我的预期输出:

Id    Label      Output
0 000010 1
1 001010 101
2 000111 111
3 111100 1111

根据上表,应该去掉左右两边的0。

最佳答案

使用str.strip:

>>> df['Output'] = df['Label'].str.strip('0')
>>> df
Id Label Output
0 0 10 1
1 1 1010 101
2 2 111 111
3 3 111100 1111
>>>

关于python - 如何在 Pandas 中删除左右字符串中的所有 '0',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56964690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com