5

For some reason, I cannot get this simple statement to work on the ñ. It seems to work on anything else but doesn't like that character. Any ideas?

DF['NAME']=DF['NAME'].str.replace("ñ","n")

Thanks

Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
user3221876
  • 275
  • 2
  • 4
  • 7
  • 1
    What is returned from the code? Does it cause an error or just fail to replace the character without spitting out an error? – Blaszard May 24 '14 at 04:27
  • This seems to work for me. Are you sure that it is the same character (has the same unicode code point)? It may just look the same... – Andy Hayden May 24 '14 at 06:14

2 Answers2

8

I'm assuming you're using Python 2.x here and this is likely a Unicode problem. Don't worry, you're not alone--unicode is really tough in general and especially in Python 2, which is why it's been made standard in Python 3.

If all you're concerned about is the ñ, you should decode in UTF-8, and then just replace the one character.

That would look something like the following:

DF['name'] = DF['name'].str.decode('utf-8').replace(u'\xf1', 'n')

As an example:

>>> "sureño".decode("utf-8").replace(u"\xf1", "n")
u'sureno'

If your string is already Unicode, then you can (and actually have to) skip the decode step:

>>> u"sureño".replace(u"\xf1", "n")
u'sureno'

Note here that u'\xf1' uses the hex escape for the character in question.

Update

I was informed in the comments that <>.str.replace is a pandas series method, which I hadn't realized. The answer to this possibly might be something like the following:

DF['name'] = map(lambda x: x.decode('utf-8').replace(u'\xf1', 'n'), DF['name'].str)

or something along those lines, if that pandas object is iterable.

Another update

It actually just occurred to me that your issue may be as simple as the following:

DF['NAME']=DF['NAME'].str.replace(u"ñ","n")

Note how I've added the u in front of the string to make it unicode.

Community
  • 1
  • 1
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
  • 2
    This question is about pandas. [str.replace](http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.core.strings.StringMethods.replace.html) is a Series method. Although, I suspect you may be dead on with the alternative code point... – Andy Hayden May 24 '14 at 06:16
  • Good point. Thanks for pointing that out; I hadn't realized. I assumed `.str` returned the string. – jdotjdot May 24 '14 at 06:29
  • `Another update` does not work. The replace value has to be like `u'\xc9'` – Tjorriemorrie Jan 12 '15 at 10:41
0

You can use replace function with special character to be replaced with a different value of your choice in the following way.

if your dataframe is df and you have to do it in all the columns that are string. in case of mine I am doing it for "\n"

df= df.applymap(lambda x: x.replace("\n"," "))
Mickael Maison
  • 25,067
  • 7
  • 71
  • 68
user8336233
  • 136
  • 1
  • 3