Categories
Python Answers

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

Spread the love

Sometimes, we want to remove unwanted parts from strings in a column with Python Pandas.

In this article, we’ll look at how to remove unwanted parts from strings in a column with Python Pandas.

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

To remove unwanted parts from strings in a column with Python Pandas, we can use the lstrip and rstrip methods.

For instance, we write

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

to call map on the values in the result column.

We call it with a lambda function that calls lstrip to remove the characters listed on the left end of the string.

Likewise, we call rstrip to remove the characters listed in the right side of the string.

Conclusion

To remove unwanted parts from strings in a column with Python Pandas, we can use the lstrip and rstrip methods.

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 *