【发布时间】:2015-05-04 16:10:08
【问题描述】:
我有一个需要转换为 JSON 的 Pandas DataFrame。 to_json() DataFrame 方法产生可接受的格式,但它将我的 DataFrame 索引转换为字符串(例如,0 变为“0.0”)。我需要“0”。
DataFrame 来自 JSON,使用 pd.io.json.read_json() 方法,将索引设置为 float64。
输入 JSON:
{"chemical": {"1": "chem2", "0": "chem1"},
"type": {"1": "pesticide", "0": "pesticide"}}
DataFrame(来自read_json()):
chemical type
0 chem1 pesticide
1 chem2 pesticide
生成的 JSON(来自 to_json()):
{"chemical": {"0.0": "chem1", "1.0": "chem2"},
"type": {"0.0": "pesticide", "1.0": "pesticide"}}
需要 JSON:
{"chemical": {"0": "chem1", "1": "chem2"},
"type": {"0": "pesticide", "1": "pesticide"}}
【问题讨论】:
标签: json python-2.7 pandas