0

I have words with \t and \r at the beginning of the words that I am trying to strip out without stripping the actual words.

For example "\tWant to go to the mall.\rTo eat something."

I have tried a few things from SO over three days. Its a Pandas Dataframe so I thought this answer pertained the best:

Pandas DataFrame: remove unwanted parts from strings in a column

But formulating from that for my own solution is not working.

i = df['Column'].replace(regex=False,inplace=False,to_replace='\t',value='')

I did not want to use regex since the expression has been difficult to make being that I am attempting to strip out '\t' and if possible also '\r'.

Here is my regular expression: https://regex101.com/r/92CUV5/5

John Veridan
  • 71
  • 1
  • 10
  • Maybe a regex is not so bad after all: `df['Column'].replace(regex=True,inplace=False,to_replace='\\t|\\r',value='')` – wp78de May 11 '18 at 05:15
  • Try just `str.replace`: `df['Column'] = df['Column'].str.replace(r'\\[tr]','')`. However, it might still affect some strings like URLs. – Wiktor Stribiżew May 11 '18 at 06:45

1 Answers1

0

Try the following code:

def remove_chars(text):
    return str(re.sub(r'[\t\r]','',text))

i = df['Column'].map(remove_chars)
Minions
  • 5,104
  • 5
  • 50
  • 91