gpt4 book ai didi

Python 2.7特殊splunk对象到数据帧的转换

转载 作者:行者123 更新时间:2023-12-01 09:32:57 27 4
gpt4 key购买 nike

我正在从 splunk 读取一些数据,我想将该格式转换为 pandas 数据帧。

Generate a pandas dataframe from ordereddict?

result = results.ResultsReader(service.jobs.export(SEARCH_COMMAND))
for rr in result:
print(rr)

OrderedDict([('H', '123'), ('U', 'aaa@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537360524/-86840158')])
OrderedDict([('H', '456'), ('U', 'sss@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537367876/-765151654')])
OrderedDict([('H', '145'), ('U', 'ddd@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537367571/540003017')])
OrderedDict([('H', '111'), ('U', 'asd@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537376045/540216322')])
OrderedDict([('H', '222'), ('U', 'asd@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537383484/-86104258')])

最佳答案

pandas.DataFrame 直接接受字典列表。

您可以通过lst = list(result)创建列表并构建数据框,如下所示。

from collections import OrderedDict
import pandas as pd

lst = [OrderedDict([('H', '123'), ('U', 'aaa@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537360524/-86840158')]),
OrderedDict([('H', '456'), ('U', 'sss@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537367876/-765151654')]),
OrderedDict([('H', '145'), ('U', 'ddd@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537367571/540003017')]),
OrderedDict([('H', '111'), ('U', 'asd@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537376045/540216322')]),
OrderedDict([('H', '222'), ('U', 'asd@global-bilgi.entp'), ('S', 'motv:SMP_SESSION_ID/1523537383484/-86104258')])]

df = pd.DataFrame(lst)

print(df)

# H U S
# 0 123 aaa@global-bilgi.entp motv:SMP_SESSION_ID/1523537360524/-86840158
# 1 456 sss@global-bilgi.entp motv:SMP_SESSION_ID/1523537367876/-765151654
# 2 145 ddd@global-bilgi.entp motv:SMP_SESSION_ID/1523537367571/540003017
# 3 111 asd@global-bilgi.entp motv:SMP_SESSION_ID/1523537376045/540216322
# 4 222 asd@global-bilgi.entp motv:SMP_SESSION_ID/1523537383484/-86104258

关于Python 2.7特殊splunk对象到数据帧的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49798372/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com