我无法将我保存的字典正确读入数据框
作为json文件。密钥似乎已损坏。不确定是否
问题是使用json.dump()
或pd.read_json()
。我想要
键是最左侧的列,但似乎混入了一个时间
数据类型。我有以下字典:
t1 = {
"666020888022790149": {
"contributors": None,
"coordinates": None,
"created_at": "Sun Nov 15 22:32:08 +0000 2015"},
"666029285002620928": {
"contributors": None,
"coordinates": None,
"created_at": "Sun Nov 15 23:05:30 +0000 2015"},
"666033412701032449": {
"contributors": None,
"coordinates": None,
"created_at": "Sun Nov 15 23:21:54 +0000 2015",
}
}
代码:
import json
import pandas as pd
with open('test1', 'w') as f:
json.dump(t1, f,
sort_keys = True,
indent=4)
df = pd.read_json('test1',orient='index')
df.head(3)
输出:
答案 0 :(得分:4)
只要告诉熊猫不要转换轴即可
result = pd.read_json('test1', orient='index', convert_axes=False)
# contributors coordinates created_at
#666020888022790149 NaN NaN 2015-11-15 22:32:08
#666029285002620928 NaN NaN 2015-11-15 23:05:30
#666033412701032449 NaN NaN 2015-11-15 23:21:54
索引的类型为字符串:
result.index.dtype
#dtype('O')
请注意,“ test1”是CSV文件的错误名称。 “ test1.csv”要好得多。