Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove substring from entire Pandas DataFrame

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading