Search code examples
pythonstringpandasnumericalphabet

How to just keep numbers and alphabets using pandas?


If my series is:

 t=pd.Series(["Payment","('TransNum', '00')","('TransNum', '07')"])

Then how to use pandas (Python) to just keep numbers and alphabets so that it is like this:

t=pd.Series(["Payment","TransNum00","TransNum07"])

Solution

  • The string operations for a pandas Series object has the replace method, which you can pass a regular expression to.

    t = t.str.replace('[^\dA-Za-z]', '')