.replace() to replace elements in a Pandas DataFrame

Michio Suginoo msuginoo at reversalpoint.com
Wed Aug 5 21:15:55 EDT 2020


Hi Bob and MRAB,
Both of you, very kind.
Thanks.
I tried with DataFrame as my original code, but this time, I inserted
regex=True and it worked.
Thank you very much!
Best


On Wed, Aug 5, 2020 at 9:50 PM Bob van der Poel <bob at mellowood.ca> wrote:

> I don't think that string.replace() has an option for a dictionary of
> replacements ... mind you, it might be nice if it did. In the meantime,
> have you tried looping through the dictionary and changing each possible
> character?
>
>
> On Wed, Aug 5, 2020 at 5:12 PM Michio Suginoo <msuginoo at reversalpoint.com>
> wrote:
>
>> Hi MRAB,
>>
>> Sorry, I simply do not understand the intention of your statement.
>> If .replace() does not do what I want to do: to replace all ' í ' with
>> 'i'
>> in any form .
>> Could you advise me other means to do it?
>>
>> I am still a learner, so I am not knowledgeable. But, I presume, this sort
>> of needs--to replace parts of string, not an entire string--should be
>> common.
>> So, I would presume, there must be some popular way to do it.
>> If you know, I would appreciate it if you could advise me.
>>
>> Thanks.
>> Best
>>
>>
>> On Wed, Aug 5, 2020 at 8:38 PM MRAB <python at mrabarnett.plus.com> wrote:
>>
>> > On 2020-08-06 00:06, Michio Suginoo wrote:
>> > > Hi MRAB,
>> > >
>> > > Here is an example:
>> > > In the original dataframe, I have something like, 'Agronomía'. And I
>> > > try to transform it to 'Agronomia'.
>> > > In this case, I try to replace í with i: the former with a Spanish
>> > > accent, the latter without.
>> > >
>> > That's what you want it to do, but what does it _actually_ do? Does it
>> > _actually_ replace characters in a string, or only match and replace
>> > entire strings?
>> >
>> > If you have a dataframe that contains the strings 'Agronomía' and 'í',
>> > does it leave 'Agronomía' as-is but replace 'í' with 'i'?
>> >
>> > On Wed, Aug 5, 2020 at 8:00 PM MRAB <python at mrabarnett.plus.com
>> > <mailto:python at mrabarnett.plus.com>> wrote:
>> > >
>> > >     On 2020-08-05 23:21, Michio Suginoo wrote:
>> > >     > Hi,
>> > >     >
>> > >     > I have trouble with .replace() method in a Pandas DataFrame.
>> > >     > My code is something like this:
>> > >     > correction_dic1 = {'á': 'a', 'í': 'i', 'ú': 'u', 'ó': 'o'}
>> > >     > df = df.replace({'Name' : correction_dic1})
>> > >     > Basically, what I am trying to do here is to replace bowels with
>> > >     Spanish
>> > >     > accent with a plain set of English alphabet in all the entries
>> > >     in the
>> > >     > column called 'Name' in the dataframe, df.
>> > >     >
>> > >     > A strange thing is that it worked perfectly in the same Jupyter
>> > >     notebook
>> > >     > this morning.
>> > >     > But all of a sudden, it started not doing the replacement any
>> more.
>> > >     >
>> > >     > Could anyone tell what could be the problem?
>> > >     >
>> > >     Does that replace characters in strings, or does it replace one
>> > >     string
>> > >     with another, e.g. replace the string 'á' with the string 'a'?
>> > >
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>> >
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
>
> --
>
> **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
> Bob van der Poel ** Wynndel, British Columbia, CANADA **
> EMAIL: bob at mellowood.ca
> WWW:   http://www.mellowood.ca
>


More information about the Python-list mailing list