这是我的json:
my_json = {
"machine_name": {
"0100": [
{
"date": "21/03/2019",
"chainage": "27760.156",
"unix_time": "1553110535",
"time": "03:35:35",
"f0001": "0.0",
"f0002": "0.0",
"f0006": "0.0",
"f0007": "0.0",
"f0008": "0.0",
"f0009": "0.0"
},
{
"date": "22/03/2019",
"chainage": "27760.156",
"unix_time": "1553110535",
"time": "03:35:35",
"f0001": "0.0",
"f0002": "0.0",
"f0006": "0.0",
"f0007": "0.0",
"f0008": "0.0",
"f0009": "0.0"
}
],
"0101": [
{
"date": "21/03/2019",
"chainage": "27761.498",
"unix_time": "1553131029",
"time": "09:17:09",
"f0001": "0.347",
"f0002": "0.007",
"f0006": "2.524",
"f0007": "0.0",
"f0008": "121.036",
"f0009": "0.0"
},
{
"date": "22/03/2019",
"chainage": "27761.498",
"unix_time": "1553131029",
"time": "09:17:09",
"f0001": "0.347",
"f0002": "0.007",
"f0006": "2.524",
"f0007": "0.0",
"f0008": "121.036",
"f0009": "0.0"
}
]
}
}
我想创建一个标头为“ date”,“ chainage”,“ unix_time”等的熊猫数据框...将“ 0100”和“ 0101”的“对象数组”合并为一个数据框。 / p>
我看过read_json和json_normalize,但是输出不是预期的。有什么想法可以达到预期的结果吗?
答案 0 :(得分:1)
>>> rows = [v[0] for k, v in my_json['machine_name'].items()]
>>> rows # I fixed up the line-wrapping here for readability.
[{'date': '21/03/2019', 'chainage': '27760.156', 'unix_time': '1553110535',
'time': '03:35:35', 'f0001': '0.0', 'f0002': '0.0', 'f0006': '0.0',
'f0007': '0.0', 'f0008': '0.0', 'f0009': '0.0'}, {'date': '21/03/2019',
'chainage': '27761.498', 'unix_time': '1553131029', 'time': '09:17:09',
'f0001': '0.347', 'f0002': '0.007', 'f0006': '2.524', 'f0007': '0.0',
'f0008': '121.036', 'f0009': '0.0'}]
这给了我们包装在单元素列表中的实际dict列表,这些列表是machine_name
下的dict值,然后我们可以从通常的表格中创建一个表:
>>> df = pd.DataFrame(rows)
并添加索引:
# we need to convert to Index explicitly from the dict_keys.
>>> index = pd.Index(my_json['machine_name'].keys())
>>> df.set_index(index, inplace=True)
结果对我来说很正确:
>>> df
chainage date f0001 f0002 ... f0008 f0009 time unix_time
0100 27760.156 21/03/2019 0.0 0.0 ... 0.0 0.0 03:35:35 1553110535
0101 27761.498 21/03/2019 0.347 0.007 ... 121.036 0.0 09:17:09 1553131029
[2 rows x 10 columns]
答案 1 :(得分:0)
以下似乎有效。
请注意,给定声明my_json
变量的方式,Python会将其作为字典而不是json字符串读取。
import pandas as pd
my_json = { ... } # you data --it will be read as a dictionary by default.
# for convenience, create this variable
d = my_json['machine_name']
# create a list to store each row (i.e.: 0100, 0101)
dict_ls = []
# loop through d and store each internal dictionary (i.e: d[0100], d[0101]...etc) in the list
for row in d.keys():
dict_ls.append(d[row][0])
# convert the list of dictionaries into a dataframe
df = pd.DataFrame(dict_ls)