我想删除 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/