熊猫使用tldex将最后2个逗号分隔的项目合并到单元格中

2024-09-29 19:20:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据帧,正在使用tldextract库。我在创建新列和连接第二个和第三个分开的字符串时遇到问题

#First 5 rows for testing purposes
df = pd.DataFrame(request['destinationhostname'].iloc[0:5])

    destinationhostname
0   pod51042psh.outlook.com
1   s.mrmserve.com
2   client-office365-tas.msedge.net
3   otf.msn.com
4   log.pinterest.com

#Applying tld extract on destinationhostname column
df['req'] = request.destinationhostname.apply(tldextract.extract)

    destinationhostname              req
0   pod51042psh.outlook.com         (pod51042psh, outlook, com)
1   s.mrmserve.com                  (s, mrmserve, com)
2   client-office365-tas.msedge.net (client-office365-tas, msedge, net)
3   otf.msn.com                     (otf, msn, com)
4   log.pinterest.com               (log, pinterest, com)

我已经尝试了很多方法来做下一部分,比如下面的方法,但是不断地出现错误

df['fld'] = df['req'].apply('.'.join[1:3])

TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'

或者

TypeError: sequence item 0: expected string, ExtractResult found

我想要的结果是:

    destinationhostname             req                                  fld
0   pod51042psh.outlook.com         (pod51042psh, outlook, com)          outlook.com
1   s.mrmserve.com                  (s, mrmserve, com)                   mrmserve.com
2   client-office365-tas.msedge.net (client-office365-tas, msedge, net)  msedge.net
3   otf.msn.com                     (otf, msn, com)                      msn.com
4   log.pinterest.com               (log, pinterest, com)                pinterest.com

Tags: comclientlogdfnetoutlookpinterestmsn
1条回答
网友
1楼 · 发布于 2024-09-29 19:20:27

切片str对象,然后join

df['fld'] = df.req.str[1:].str.join('.')

df

               destinationhostname                                  req            fld
0          pod51042psh.outlook.com          (pod51042psh, outlook, com)    outlook.com
1                   s.mrmserve.com                   (s, mrmserve, com)   mrmserve.com
2  client-office365-tas.msedge.net  (client-office365-tas, msedge, net)     msedge.net
3                      otf.msn.com                      (otf, msn, com)        msn.com
4                log.pinterest.com                (log, pinterest, com)  pinterest.com

或者作为@coldspeed has shown,可以使用数组结尾引用进行切片

df['fld'] = df.req.str[-2:].str.join('.')

相关问题 更多 >

    热门问题