Home > Mobile >  Remove substring from entire Pandas DataFrame
Remove substring from entire Pandas DataFrame

Time:03-03

I have a Pandas DataFrame with three columns and every string in these columns ends with '\n'

Example:

test\n | abc\n | 123\n

Now, I want to get rid of these '\n' and achieve:

test | abc | 123

I only find solutions where substrings are removed from certain columns, but I want to remove this substring from the entire DataFrame

I would therefore be very happy if someone had a solution to my problem.

Thank you very much in advance for your help!

CodePudding user response:

Use:

df = df.apply(lambda s: s.str.strip())

To limit to a list of columns:

# cols = ['col1', 'col2']
df[cols] = df[cols].apply(lambda s: s.str.strip())
  • Related