Remove substring from entire Pandas DataFrame

Viewed 146

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!

1 Answers

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