键列表:来自DataFrame列的值
我搜索一个“ global ”解决方案,从数据框的列,“键”:“ value” 的列表中提取,使每个“ key” 作为列名和“ value ”作为值:
之前:
id, severity, user, events, city
1,Low,test1,[{'type': 'AAA', 'timestamp': 1653135398011, 'agent': None,...}], Athens
2,Medium,test2,[{'type': 'BBB', 'timestamp': 1653135398012, 'agent': STIX,...}], Buffalo
3,,test3,[{'type': 'CCC', 'timestamp': 1653135398013, 'agent': ACQ,...}], Carson
4,Low,test4,[{'type': 'DDD', 'timestamp': 1653135398014, 'agent': VTC,...}], Detroit
after:
id, severity, user, type, timestamp, agent,..., city
1,Low,test1,AAA,1653135398011,None, ..., Athens
2,Medium,test2,BBB,1653135398012,STIX, ..., Buffalo
3,,test3,CCC,1653135398013,ACQ,..., Carson
4,Low,test4,DDD,1653135398014,VTC,..., Detroit
在stackoverflow上以其名称提取2或3个字段,但是如果我们不知道列表内容,那么如何提取所有内容? 我认为Lambda功能和/或Regex会完成这项工作,但我的技能太糟糕了...
感谢您的帮助
I search a "global" solution to extract, from a dataframe's column, a list of "key":"value" to have each "key" as Column name and "value" as Value:
Before:
id, severity, user, events, city
1,Low,test1,[{'type': 'AAA', 'timestamp': 1653135398011, 'agent': None,...}], Athens
2,Medium,test2,[{'type': 'BBB', 'timestamp': 1653135398012, 'agent': STIX,...}], Buffalo
3,,test3,[{'type': 'CCC', 'timestamp': 1653135398013, 'agent': ACQ,...}], Carson
4,Low,test4,[{'type': 'DDD', 'timestamp': 1653135398014, 'agent': VTC,...}], Detroit
After:
id, severity, user, type, timestamp, agent,..., city
1,Low,test1,AAA,1653135398011,None, ..., Athens
2,Medium,test2,BBB,1653135398012,STIX, ..., Buffalo
3,,test3,CCC,1653135398013,ACQ,..., Carson
4,Low,test4,DDD,1653135398014,VTC,..., Detroit
On stackoverflow some solution extract 2 or 3 fields by their names, but if we don't know list content, how extract everything?
I think lambda function and/or regex will do the job but my skills are too bad...
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试这样的事情,
我让它与看起来像这样的
dataFrame
使用,编辑:
谢谢@thyebri的建议。无需使用循环即可完成相同的操作。尽管我没有资格说这是否效率或多或少。
You can try something like this
I got it working with a
DataFrame
that looks like this,Edit:
Thank @Thyebri for the suggestion. It's possible to complete the same without using a loop. Though I am not qualified to say if it's more or less efficient.
这是做您问题提出的方法的方法:
说明:
functools.Reduce()
,在Events
列中创建一个字典对象列表evest> evest> Events
使用from_records()创建的列中的字典中的列的数据帧,以及(3)列(3)(3) 的权利上s )
在
事件 问题,例如围绕“代理”值(Stix,ACQ,VTC)的引号。
Here is a way to do what your question asks:
Explanation:
functools.reduce()
, create a list of the dictionary objects in theevents
columnevents
column created using from_records(), and (3) the column(s) to the right ofevents
(in this case, justcity
)Full test code:
Output:
NOTE: It was necessary to make slight changes to the dataframe shown in the question, such as putting quotes around the 'agent' values (STIX, ACQ, VTC).