Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

I am looking for an efficient way to remove unwanted parts from strings in a DataFrame column.

Data looks like:

    time    result

1    09:00   +52A

2    10:00   +62B

3    11:00   +44a

4    12:00   +30b

5    13:00   -110a

I need to trim these data to:

    time    result

1    09:00   52

2    10:00   62

3    11:00   44

4    12:00   30

5    13:00   110

I tried .str.lstrip('+-') and .str.rstrip('aAbBcC'), but got an error:

TypeError: wrapper() takes exactly 1 argument (2 given)

Any pointers would be greatly appreciated!

1 Answer

0 votes
by (41.4k points)
edited by

This line of code will remove unwanted parts from strings in a column:

data['result'] = data['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC'))

If you want to learn Python proogramming language for Data Science then you can watch this complete video tutorial:

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...