Python Forum
pandas dataframe.replace regex
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas dataframe.replace regex
#1
Dear Pandas Experts,
I am trying to replace occurences like "United Kingdom of Great Britain and Ireland" or "United Kingdom of Great Britain & Ireland"
with just "United Kingdom". So I thought I use a regex to look for strings that contain "United Kingdom".
However, my two attempts below do not work:

    dftwo['Country'].replace(r'^United Kingdom of Great Britain','United Kingdom',inplace=True, regex=True)

    dftwo['Country'].replace('/United Kingdom/','United Kingdom',inplace=True, regex=True)
I would really appreciate any help!
Reply
#2
You have no "wildcards" there

Output:
In [39]: import pandas as pd In [40]: df = pd.DataFrame({"country":["United Kingdom of Great Britain", "Ireland", "United Kingdom of Great Britain & Ireland"], "value":[12,31, 43]}) In [41]: df Out[41]:                                      country  value 0            United Kingdom of Great Britain     12 1                                    Ireland     31 2  United Kingdom of Great Britain & Ireland     43 In [42]: df.country.replace("^United Kingdom of Great Britain.*", "United Kingdom", regex=True, inplace=True) In [43]: df Out[43]:           country  value 0  United Kingdom     12 1         Ireland     31 2  United Kingdom     43
Reply
#3
Hi zivoni!
Thank you so much. I had no idea that I could use a wild card there!
Reply
#4
"wildcard" is little vague   - pandas' replace is made on top of standard python re.sub, so you can use exactly same regular expressions you would use for re.sub and python re documentation is your friend.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas dataframe indexing pythonNovice 1 990 Jun-16-2022, 04:43 PM
Last Post: jefsummers
  How can I convert specific rows from excel to pandas dataframe? mcva 1 1,795 Apr-20-2020, 09:14 AM
Last Post: pyzyx3qwerty
  Histogram using pandas dataframe not showing proper output ift38375 1 2,182 Jul-04-2019, 10:43 PM
Last Post: scidam
  Pandas| iterrows | csv.replace BeerLover 1 4,587 May-19-2017, 05:54 PM
Last Post: buran
  pandas dataframe next rows value metalray 2 10,138 Mar-06-2017, 11:31 AM
Last Post: metalray
  pandas dataframe group by count index metalray 5 10,266 Mar-01-2017, 09:14 AM
Last Post: metalray
  pandas dataframe substracting columns: key error metalray 2 7,023 Feb-24-2017, 07:59 AM
Last Post: metalray

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020