如何使用正则表达式提取大熊猫中的字符串?

时间:2018-06-27 09:37:25

标签: python regex

我有一个数据框:

游泳(4) 1 4远足(1) 2 2跑步(12) 3 5钓鱼(2)

|     |  sid  | Hobby (times per month) |
|-----+-------+-------------------------|
|  0  |   3   |      swimming(4)        |
|-----+-------+-------------------------|
|  1  |   4   |      hiking  (1 )       |
|-----+-------+-------------------------|
|  2  |   2   |      running ( 12 )     |
|-----+-------+-------------------------|
|  3  |   5   |      fishing ( 2 )      |

如何通过删除第二列中的括号来提取字符串:

|     |  sid  | Hobby (times per month) |
|-----+-------+-------------------------|
|  0  |   3   |        swimming         |
|-----+-------+-------------------------|
|  1  |   4   |        hiking           |
|-----+-------+-------------------------|
|  2  |   2   |        running          |
|-----+-------+-------------------------|
|  3  |   5   |        fishing          |

3 个答案:

答案 0 :(得分:1)

例如,如果您希望将swimming(4)更改为swimming,则可以使用以下正则表达式:

^([\w]+)[\s]*\([\s]*[\d]*[\s]*\)[\s]*$

演示:https://regex101.com/r/sTO1Q9/1

测试用例:

swimming(4)
hiking   (1 )
running ( 12 )
fishing( 2 )
hiking(1) 

匹配:

Match 1
Full match  0-11    `swimming(4)`
Group 1.    0-8 `swimming`
Match 2
Full match  12-25   `hiking   (1 )`
Group 1.    12-18   `hiking`
Match 3
Full match  26-40   `running ( 12 )`
Group 1.    26-33   `running`
Match 4
Full match  41-53   `fishing( 2 )`
Group 1.    41-48   `fishing`
Match 5
Full match  54-64   `hiking(1) `
Group 1.    54-60   `hiking`

答案 1 :(得分:1)

您可以使用'str'方法来匹配熊猫中的字符串

df.columns = ['sid','Hobby']
df.Hobby = df.Hobby.str.extract(r'(\w*)')

答案 2 :(得分:0)

要在pandas中实现正则表达式,可以使用pandas.apply():

import re

def remove_brackets(string):
    part = regexp_matcher.findall(string)
    if not part:
        return string
    return part[0]

regexp_matcher = re.compile(r'^([\w]+)[\s]*\([\s]*[\d]*[\s]*\)[\s]*$')
df = pd.DataFrame()
df['string'] = ['swimming(4)', 'swimming(4)', 'swimming(4)']    
df['new_string'] = df['string'].apply(remove_brackets)