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
#6
Is your Geo column converted to a string? df.Geo.apply(str) does not work in place and from your output it does not look that you actually converted it (no quotes, but it could be interpreter).

This worked for me:
Output:
In [176]: df = pd.DataFrame({'Geo':["{'coordinates': [39.11890951, -84.48903638], 'type': 'Point'}"]}) In [177]: df.loc[0, "Geo"] Out[177]: "{'coordinates': [39.11890951, -84.48903638], 'type': 'Point'}" In [178]: pattern = r".*\[(-*\d+\.\d+), (-*\d+\.\d+)\].*" In [179]: df.Geo = df.Geo.str.replace(pattern, r"\1 \2") In [180]: df.Geo[0] Out[180]: '39.11890951 -84.48903638'
If you want to extract coordinates as seperate columns, you will need another split and convert to number. Or you can extract coordinates separately:
df['long'] = np.float(df.Geo.str.replace(pattern, r"\1"))
df['lat'] = np.float(df.Geo.str.replace(pattern, r"\2"))
My re knowlodge is rudimentary, so its possible that the pattern I have used isnt the right one ....

EDIT: yes, if your column consists of dict/list, its much better to do it with direct access
Reply


Messages In This Thread
RE: Removing characters from columns in data frame - by zivoni - Mar-07-2017, 06:18 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