我得到了嵌套了dict的数据。
我正在尝试使用json_normalize
转换为数据帧,但出现错误。
d = ['{"appMetadata": {"index": "cfs_ccs_eddi_35725", "host": "iaasn00009634", "job": "splunk_scraper"}, "timestampEpochSecond": 1545172308711, "metricTags": {"source": "/local/apps/eddi/presentment/logs/eddi-http-presentment-requests.log"}, "metricName": "splunk.logs.tstats.count.per.min", "metricValue": 5, "metricType": "count"}']
df = pd.DataFrame.from_dict({(i,j): d[i][j]
for i in d.keys()
for j in d[i].keys()},
orient='index')
错误是:
AttributeError:“列表”对象没有属性“键”
答案 0 :(得分:0)
将d列表转换为字典
d = dict(itertools.zip_longest(*[iter(l)] * 2, fillvalue=""))
答案 1 :(得分:0)
list
,里面有str
,而不是dict
或json
:dict
部分由引号['{bunch_of_stuff}']
ast.literal_eval
d = ['{"appMetadata": {"index": "cfs_ccs_eddi_35725", "host": "iaasn00009634", "job": "splunk_scraper"}, "timestampEpochSecond": 1545172308711, "metricTags": {"source": "/local/apps/eddi/presentment/logs/eddi-http-presentment-requests.log"}, "metricName": "splunk.logs.tstats.count.per.min", "metricValue": 5, "metricType": "count"}']
from ast import literal_eval
d = literal_eval(d[0])
print(d)
d
的输出:{'appMetadata': {'index': 'cfs_ccs_eddi_35725',
'host': 'iaasn00009634',
'job': 'splunk_scraper'},
'timestampEpochSecond': 1545172308711,
'metricTags': {'source': '/local/apps/eddi/presentment/logs/eddi-http-presentment-requests.log'},
'metricName': 'splunk.logs.tstats.count.per.min',
'metricValue': 5,
'metricType': 'count'}
pandas.io.json.json_normalize
:from pandas.io.json import json_normalize
import pandas as pd
df = json_normalize(d)
print(df)
df
的输出: timestampEpochSecond metricName metricValue metricType appMetadata.index appMetadata.host appMetadata.job metricTags.source
1545172308711 splunk.logs.tstats.count.per.min 5 count cfs_ccs_eddi_35725 iaasn00009634 splunk_scraper /local/apps/eddi/presentment/logs/eddi-http-presentment-requests.log