如何从dataFrame中的字典中的每行中提取文本?
我一直在尝试长时间的方法,似乎无法找到一种从字符串中提取浏览器的简洁方法。它是DF中的一列,因此需要在
列看起来像该列的所有行上迭代该
0 [{'name': 'Chrome', 'version': '36.0.1985.143'}]
1 [{'name': 'Chrome', 'version': '34.0.1847.137'}]
2 [{'name': 'Chrome', 'version': '29.0.1547.76'}]
3 [{'name': 'Chrome', 'version': '33.0.1750.154'}]
4 [{'name': 'Chrome', 'version': '36.0.1985.143'}]
列,称为浏览器。
我尝试了以下内容。
df_agent_info['browser'].str.split("\[\{\'[a\-z]\'")
还有其他更糟糕的例子。感谢您的帮助。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
(? >
(?<='name': ')
is a positive lookbehind: it looks for matches that follow in this case'name': '
给定:
让我们以Python的形式评估它们:
现在我们可以轻松提取它:
输出:
Given:
Let's evaluate them as python:
Now we can extract it easily:
Output:
首先将字符串转换为包含使用内置
ast.literal_eval
的dict的列表(比使用eval
)更安全,然后获取每个值的“名称”值使用list_dict [0] ['name']
使用字典。将此逻辑应用于浏览器
列的每个字符串值,使用series.apply
。将所有内容放在一起:
输出:
First convert the strings to lists containing a dict using the built-in
ast.literal_eval
(it is safer than usingeval
), and then get the 'name' value of each dictionary usinglist_dict[0]['name']
. Apply this logic to each string value of thebrowser
column usingSeries.apply
.Putting all together:
Output:
这是另一种方法,
,使用REGEX组
使用REGEX组
here is another way to do it, One liner
using regex groups to match for content b/w the astrophes, by using regex groups