从字符串列中提取反斜杠之间的值。

huangapple go评论83阅读模式
英文:

Extract Value between backslashes from string column

问题

使用Python 3.10,在一个包含以URL字符串存储的值的列中,我想提取两个反斜杠之间的数字值,以及一个结构化列中的行ID。

在反斜杠之前的值是有条件的,可能是"DomainId"、"DomainainSiteId"等等,URL的长度和字符也可能略有不同。最后,反斜杠之间的数字值的长度可能在5到9个字节之间变化,但始终位于反斜杠之间。

import pandas as pd

# 示例数据
data = {'id': [1, 2, 3],
        'url': ['https://company.db.abcd.com/DomainId/123456789/Other',
                'https://company.db.abcd.com/DomainainSiteId/123456/Other',
                'https://companyaddedwords.db.abcd.com/DomainId/1234567/Other']}

df = pd.DataFrame(data)

# 使用正则表达式提取数字值
df['DomainId'] = df['url'].str.extract(r'/(\d+)/', expand=False)
df['DomainSiteId'] = df['url'].str.extract(r'/DomainainSiteId/(\d+)/', expand=False)

# 填充NaN
df = df.fillna('')

# 选择需要的列
result_df = df[['id', 'DomainId', 'DomainSiteId']]
print(result_df)

期望输出:

   id   DomainId DomainSiteId
0   1  123456789             
1   2     123456        123456
2   3    1234567

当前输出:

   id   DomainId DomainSiteId
0   1  123456789             
1   2     123456        123456
2   3    1234567
英文:

Using Python 3.10 have a column that has values stored in a URL string, I'm looking to extract the numeric value between two backslashes as well as the row id which is in a structured column.

The value that proceeds the backslashes is conditional and could be, "DomainId", "DomainainSiteId" etc. and the url may may also vary slightly in length and characters. Lastly, the length of the numeric value between the backslashes may vary 5 - 9 bytes, but will always be between slashes.

id url
https://company.db.abcd.com/DomainId/123456789/Other https://company.db.abcd.com/DomainainSiteId/123456/Other https://companyaddedwords.db.abcd.com/DomainId/1234567/Other

Work in process
df.url.str.extract('\w/(?P<DomainId>.+)\Z', expand=True)

Can't seem to figure out the terminators to pull the numeric value only with Regex wondering if findall is a better option

Expected Output
id  DomainId  DomainSiteId  
1   123456789  
2              123456  
3   1234567

Current Output
DomainId
DomainId/123456789/Other
DomainSightId/123456/Other
DomainId/1234567/Other

答案1

得分: 1

你可以使用两个命名捕获组,使用 (?P<groupname>...) 标记,并使用 | 进行交替匹配以捕获这两种变体:

/DomainId/(?P<DomainId>\d{5,9})\b|/DomainainSiteId/(?P<DomainainSiteId>\d{5,9})\b

正则表达式示例:

pattern = r'/DomainId/(?P<DomainId>\d{5,9})\b|/DomainainSiteId/(?P<DomainainSiteId>\d{5,9})\b'
df = df.url.str.extract(pattern).fillna('')
print(df)

输出:

        DomainId DomainainSiteId
    0  123456789                
    1                     123456
    2    1234567

正则表达式演示

英文:

You can use 2 named capture groups denoted with (?P&lt;groupname&gt;...) and use an alternation with | to capture both variants:

/DomainId/(?P&lt;DomainId&gt;\d{5,9})\b|/DomainainSiteId/(?P&lt;DomainainSiteId&gt;\d{5,9})\b

Regex demo

pattern = r&#39;/DomainId/(?P&lt;DomainId&gt;\d{5,9})\b|/DomainainSiteId/(?P&lt;DomainainSiteId&gt;\d{5,9})\b&#39;
df = df.url.str\
    .extract(pattern)\
    .fillna(&#39;&#39;)
print(df)

Output

    DomainId DomainainSiteId
0  123456789                
1                     123456
2    1234567 

答案2

得分: 0

你可以尝试使用.str.extract然后对数据框进行透视:

x = df['url'].str.extract(r'([^/]+)/(\d{5,9})')
print(x.pivot(columns=0, values=1).fillna(''))

输出:

0   DomainId DomainainSiteId
0  123456789                
1                     123456
2    1234567                

初始数据框:

                                                            url
0          https://company.db.abcd.com/DomainId/123456789/Other
1      https://company.db.abcd.com/DomainainSiteId/123456/Other
2  https://companyaddedwords.db.abcd.com/DomainId/1234567/Other
英文:

You can try to use .str.extract and then pivot the dataframe:

x = df[&#39;url&#39;].str.extract(r&#39;([^/]+)/(\d{5,9})&#39;)
print(x.pivot(columns=0, values=1).fillna(&#39;&#39;))

Prints:

0   DomainId DomainainSiteId
0  123456789                
1                     123456
2    1234567                

Initial dataframe:

                                                            url
0          https://company.db.abcd.com/DomainId/123456789/Other
1      https://company.db.abcd.com/DomainainSiteId/123456/Other
2  https://companyaddedwords.db.abcd.com/DomainId/1234567/Other

答案3

得分: 0

你可以使用以下模式来捕获两个值。

//.+?/(.+?)/(\d+)/

输出

DomainId, 123456789
DomainainSiteId, 123456
DomainId, 1234567
英文:

You can use the following pattern to capture both values.

//.+?/(.+?)/(\d+)/

Output

DomainId, 123456789
DomainainSiteId, 123456
DomainId, 1234567

huangapple
  • 本文由 发表于 2023年6月13日 04:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定