Python Forum
Removing characters from columns in data frame
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing characters from columns in data frame
#13
For me it works quite well.
Output:
In [4]: a = '"{u\'type\': u\'Point\', u\'coordinates\': [43.18040111, -79.02213645]}"' In [5]: df = pd.DataFrame({"geo":[eval(a)]}) In [6]: df Out[6]:                                                  geo 0  {u'type': u'Point', u'coordinates': [43.180401... In [7]: repr(df.geo[0]) Out[7]: '"{u\'type\': u\'Point\', u\'coordinates\': [43.18040111, -79.02213645]}"' In [8]: pattern = r".*\[(-*\d+\.\d+), (-*\d+\.\d+)\].*" In [9]: df['long'] = np.float(df.geo.str.replace(pattern, r"\1")) In [10]: df Out[10]:                                                  geo       long 0  {u'type': u'Point', u'coordinates': [43.180401...  43.180401
It looks that your string is actually representation of geo dictionary, it would be easier if you extracted long/lat from dictionary before converting it to a string.
Reply


Messages In This Thread
RE: Removing characters from columns in data frame - by zivoni - Apr-15-2017, 06:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Grouping in pandas/multi-index data frame Aleqsie 3 920 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  Filtering Data Frame, with another value NewBiee 9 1,686 Aug-21-2023, 10:53 AM
Last Post: NewBiee
  Deleting characters between certain characters stahorse 7 1,369 Jul-03-2023, 12:59 AM
Last Post: Pedroski55
  Exporting data frame to excel dyerlee91 0 1,720 Oct-05-2021, 11:34 AM
Last Post: dyerlee91
  Pandas Data frame column condition check based on length of the value aditi06 1 2,834 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  Adding a new column to a Panda Data Frame rsherry8 2 2,233 Jun-06-2021, 06:49 PM
Last Post: jefsummers
  import columns of data from local csv file CatherineKan 2 3,497 May-10-2021, 05:10 AM
Last Post: ricslato
  pandas.to_datetime: Combine data from 2 columns ju21878436312 1 2,549 Feb-20-2021, 08:25 PM
Last Post: perfringo
  grouped data frame glitter 0 1,678 Feb-02-2021, 11:22 AM
Last Post: glitter
  how to filter data frame dynamically with the columns psahay 0 2,483 Aug-24-2020, 01:10 PM
Last Post: psahay

Forum Jump:

User Panel Messages

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