简体   繁体   中英

Slicing a column in a dataframe with varied number of characters in each row (Python)

The column I would like to slice looks like this:

{'name':['A', 'B', 'C'], 'location':['(x=31.33 y=19.98)', '(x=9.33 y=6.98)', '(x=-12.67 y=-30.02)']} 

I would like to pull the x and y values into their own columns to look like this:

{'name':['A', 'B', 'C'], 'x':[31.33, 9.33, -12.67], 'y':[19.98,6.98,-30.02]} 

I am assuming I need to do some slicing, but am unsure how to go about it. Thanks.

You can use regex for this:

import re

d = {'name':['A', 'B', 'C'], 'location':['(x=31.33 y=19.98)', '(x=9.33 y=6.98)', '(x=-12.67 y=-30.02)']} 

x = [re.search(r'x=((?:\-)?\d+(?:\.\d+))', x).group(1) for x in d['location']]
y = [re.search(r'y=((?:\-)?\d+(?:\.\d+))', x).group(1) for x in d['location']]

res = {
    'name': d['name'],
    'x': list(map(float, x)),
    'y': list(map(float, y))
}

print(res)
# {'name': ['A', 'B', 'C'], 'x': [31.33, 9.33, -12.67], 'y': [19.98, 6.98, -30.02]}

In case you are very sure about your data that they always follow this pattern, you can simplify above regex to:

x = [re.search(r'x=(.*) ', x).group(1) for x in d['location']]
y = [re.search(r'y=(.*)\)', x).group(1) for x in d['location']]

Here's a solution:

start = {
    'name':['A', 'B', 'C'],
    'location':['(x=31.33 y=19.98)',
    '(x=9.33 y=6.98)',
    '(x=-12.67 y=-30.02)']
    } 

xList = []
yList =  []
for string in start['location']:
    splitted = string[1:-1].split(" ")
    x = splitted[0].split("=")[1]
    y = splitted[1].split("=")[1]
    xList.append(x)
    yList.append(y)

end = {
    'name' : start['name'],
    'x' : xList,
    'y' : yList
}

print(end)

You can also use regexes to match patterns in strings ( documentation , regex expressions testing website )

EDIT:

Here's a solution with a regex, much more elegant:


import re
start = {
    'name':['A', 'B', 'C'],
    'location':['(x=31.33 y=19.98)',
    '(x=9.33 y=6.98)',
    '(x=-12.67 y=-30.02)']
    } 

end = {
    'name' : start['name'],
    'x' : [],
    'y' : []
}

for string in start['location']:
    checkNumber = re.compile("([\d]+[.]*[\d]*)")
    numbers = checkNumber.findall(string)
    end['x'].append(numbers[0])
    end['y'].append(numbers[1])


print(end)

You can test the regex here

You can do this a little more elegantly with the re library (and list comprehensions).

import re 

data = {'name':['A', 'B', 'C'], 'location':['(x=31.33 y=19.98)', '(x=9.33 y=6.98)', '(x=-12.67 y=-30.02)']}

data['x'] = [float(re.split("=| |\)", i)[1]) for i in data['location']]
data['y'] = [float(re.split("=| |\)", i)[3]) for i in data['location']]

del(data['location'])

data
>>> {'name': ['A', 'B', 'C'],
'x': [31.33, 9.33, -12.67],
'y': [19.98, 6.98, -30.02]}

You need to parse the string:

import pandas as pd
import re

t = {'name':['A', 'B', 'C'], 'location':['(x=31.33 y=19.98)', '(x=9.33 y=6.98)', '(x=-12.67 y=-30.02)']} 

res = pd.DataFrame({'name':t['name'], 'x':[float(re.search("\(x=(.*) y", i).group(1)) for i in t['location']], 'y':[float(re.search("y=(.*)\)", i).group(1)) for i in t['location']]})


The easiest way is to create new columns using `pandas.Series.str.extract()', ie.:

df = pd.DataFrame(["{'name':['A', 'B', 'C'], 'location':['(x=31.33 y=19.98)', '(x=9.33 y=6.98)', '(x=-12.67 y=-30.02)']}"])
df.location.str.extract(r'x=(?P<x>[0-9.-]+) y=(?P<y>[0-9.-]+)', expand=True)

Output:

        x       y
0   31.33   19.98
1    9.33    6.98
2  -12.67  -30.02

And if you need to save the new columns in the existing dataframe you can use pd.concat() , ie.:

df = pd.concat([df, df.location.str.extract(r'x=(?P<x>[0-9.-]+) y=(?P<y>[0-9.-]+)', expand=True)], axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM