[英]Set Timestamp Column in CSV as Index and Parse Dates Using Python and Pandas
[英]Python Pandas to CSV using custom column as index
我使用下面的代码创建了一个 Pandas 数据框并输出到 JSON:
dataDict['grant_id'] = list(grant_ids)
dataDict['patent_title'] = list(patent_title)
dfjson = dfjson.transpose()
dfjson = pd.DataFrame.from_dict(dataDict, orient='index')
output_json =dfjson.to_json('output_json.json')
{"0":
{"grant_id":"US10357643",
"patent_title":"System for enhanced sealing of coupled medical flui,
.... }}
但是,我不希望 0 包含在 JSON 中。 我希望它看起来像这样:
{"US10357643":{
"patent_title":"System for enhanced sealing of coupled medical flui,
.... }}
我想使用grant_id
作为索引,而不是 0, 1, 2, 3, ... 有没有办法解决这个问题?
我相信你需要带有双转置的DataFrame.set_index
:
dfjson = pd.DataFrame({"0":{"grant_id":"US10357643",
"patent_title":"System1"},
"1":{"grant_id":"US10357644",
"patent_title":"System2"}})
print (dfjson)
0 1
grant_id US10357643 US10357644
patent_title System1 System2
output_json = dfjson.T.set_index('grant_id').T.to_json()
print (output_json)
{"US10357643":{"patent_title":"System1"},"US10357644":{"patent_title":"System2"}}
dfjson.T.set_index('grant_id').T.to_json('output_json.json')
所以似乎需要在转置之前创建索引:
dataDict['grant_id'] = list(grant_ids)
dataDict['patent_title'] = list(patent_title)
dfjson = pd.DataFrame.from_dict(dataDict, orient='index')
dfjson = dfjson.set_index('grant_id').transpose()
dfjson.to_json('output_json.json')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.