Remove opening and closing parenthesis with word i

2020-05-06 12:48发布

问题:

Given a data frame:

df = 

                         multi
0 MULTIPOLYGON(((3 11, 2 33)))
1 MULTIPOLYGON(((4 22, 5 66)))

I was trying to remove the word 'MULTIPOLYGON', and parenthesis '(((', ')))'


My try:

df['multi'] = df['multi'].str.replace(r"\(.*\)","")
df['multi'] = df['multi'].map(lambda x: x.lstrip('MULTIPOLYGON()').rstrip('aAbBcC'))

df.values = 

array([[''],
       [''],
       ...
       [''],
       [''],
       [''],
       ['7.5857754821 44.9628409423']

Desired output:

df = 

     multi
3 11, 2 33
 4 22, 5 6

回答1:

Try this:

    import pandas as pd
import re 
def f(x):
    x = ' '.join(re.findall(r'[0-9, ]+',x))
    return x

def f2(x):
    x = re.findall(r'[0-9, ]+',x)

    return pd.Series(x[0].split(','))       


df =pd.DataFrame({'a':['MULTIPOLYGON(((3 11, 2 33)))' ,'MULTIPOLYGON(((4 22, 5 6)))']})
df['a'] = df['a'].apply(f)
print(df)
#or for different columns you can do
df =pd.DataFrame({'a':['MULTIPOLYGON(((3 11, 2 33)))' ,'MULTIPOLYGON(((4 22, 5 6)))']})
#df['multi'] = df.a.str.replace('[^0-9. ]', '', regex=True)
#print(df)
list_of_cols = ['c1','c2']
df[list_of_cols] = df['a'].apply(f2)
del df['a']
print(df)

output:

            a
0  3 11, 2 33
1   4 22, 5 6
     c1     c2
0  3 11   2 33
1  4 22    5 6
[Finished in 2.5s]


回答2:

You can also use str.replace with a regex:

# removes anything that's not a digit or a space or a dot
df['multi'] = df.multi.str.replace('[^0-9\. ]', '', regex=True)#changing regex


回答3:

You can use df.column.str in the following way.

df['a'] = df['a'].str.findall(r'[0-9.]+')
df = pd.DataFrame(df['a'].tolist())
print(df)

output:

     0     1
0  3.49  11.10
1  4.49  22.12

This will work for any number of columns. But in the end you have to name those columns.

df.columns = ['a'+str(i) for i in range(df.shape[1])]

This method will work even when some rows have different number of numerical values. like

df =pd.DataFrame({'a':['MULTIPOLYGON(((3.49)))' ,'MULTIPOLYGON(((4.49 22.12)))']})

     a
 0  MULTIPOLYGON(((3.49)))
 1  MULTIPOLYGON(((4.49 22.12)))

So the expected output is

      0     1
0   3.49    None
1   4.49    22.12

After naming the columns using,

df.columns = ['a'+str(i) for i in range(df.shape[1])]

You get,

      a0    a1
0   3.49    None
1   4.49    22.12


回答4:

Apply is a rather slow method in pandas since it's basically a loop that iterates over each row and apply's your function. Pandas has vectorized methods, we can use str.extract here to extract your pattern:

df['multi'] = df['multi'].str.extract('(\d\.\d+\s\d+\.\d+)')

        multi
0  3.49 11.10
1  4.49 22.12