Categories
Python Answers

How to remove unwanted parts from strings in a column with Python Pandas?

Spread the love

To remove unwanted parts from strings in a column with Python Pandas, we can use the map method.

For instance, we write

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

to call map with a lambda function that returns the original string with the unwanted parts removed with lstrip and rstrip.

And then we assign the returned results back to the result column

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *