我有一个熊猫数据帧,正在使用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
切片
str
对象,然后join
或者作为@coldspeed has shown,可以使用数组结尾引用进行切片
相关问题 更多 >
编程相关推荐