Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

I want to delete all '0's in a string in left and right in pandas dataframe.

Here's my data set:

Id    Label

0     000010

1     001010

2     000111

3     111100

Here's my expected output:

Id    Label      Output

0     000010          1

1     001010        101

2     000111        111

3     111100       1111

Based on the table above, it is expected that any 0's in left and right are removed.

1 Answer

0 votes
by (16.8k points)

You can use 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

Related questions

Browse Categories

...