Remove unwanted parts from strings in a column

Yannan Wang picture Yannan Wang · Dec 3, 2012 · Viewed 311.5k times · Source

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!

Answer

eumiro picture eumiro · Dec 3, 2012
data['result'] = data['result'].map(lambda x: x.lstrip('+-').rstrip('aAbBcC'))