Skip to content

Instantly share code, notes, and snippets.

@smram
Created October 4, 2017 23:35
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save smram/d6ded3c9028272360eb65bcab564a18a to your computer and use it in GitHub Desktop.
Save smram/d6ded3c9028272360eb65bcab564a18a to your computer and use it in GitHub Desktop.
[pandas] replace newlines,tabs,carriage returns in fields
# got to handle both escaped and literal
df.replace(to_replace=[r"\\t|\\n|\\r", "\t|\n|\r"], value=["",""], regex=True, inplace=<INPLACE>)
@emesterhazy
Copy link

Thanks! I originally tried using replace without regex, but that didn't seem to work. Maybe python was treating it as a string literal or something.

@edips
Copy link

edips commented Dec 19, 2018

Thank you very much. I tried so many tricks about it from stackoverflow, none of them worked, only your script works.

@olaCT
Copy link

olaCT commented Mar 14, 2019

very helpful !

@GwenVCX
Copy link

GwenVCX commented Oct 27, 2019

Thank you! I'll add a reference to this in the Stack Overflow answer LOL

@nmonroygt
Copy link

Thanks a lot!

@tiagofer
Copy link

Thank you!!!

@hargurjeet
Copy link

what values needs to be passed at 'inplace'

@smram
Copy link
Author

smram commented Apr 26, 2020

@JacobJWalker
Copy link

This is great! It is becoming part of the standard data wrangling that I do!

@jvioladevelops
Copy link

can you break down this code or provide a reference? Thank you!

@kwright15
Copy link

This also worked for me! And I had also tried many suggestions from StackOverflow that did not work. Thank you!

@sktzofrenic
Copy link

I got typeError: Cannot compare types 'ndarray(dtype=int64)' and 'str' on one of the rows in the database table

@MarceloCorrales
Copy link

with inplace=False worked like a charmed, with =True it didn't let me do the "to_CSV". anyway, problem solved!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment