[英]convert json file to data frame in python
I am new to json file handling.我是 json 文件处理的新手。 I have a below json file
我有一个下面的json文件
{"loanAccount":{"openAccount":[{"accountNumber":"986985874","accountOpenDate":"2020-02-045-11:00","accountCode":"NA","relationship":"Main account","loanTerm":"120"}]}}
I want to convert this into dataframe.我想将其转换为数据帧。 I am using the below code :
我正在使用以下代码:
import pandas as pd
from pandas.io.json import json_normalize
data1 = pd.read_json (r'./DLResponse1.json',lines=True)
df = pd.DataFrame.from_dict(data1, orient='columns')
This is giving me the below output :这给了我以下输出:
index loanAccount
0 {'openAccount': [{'accountNumber': '986985874', 'accountOpenDate': '2020-02-045-11:00', 'accountCode': 'NA', 'relationship': 'Main account', 'loanTerm': '120'}]}}
However I want to extract in the below format :但是我想以以下格式提取:
loanAccount openAccount accountNumber accountOpenDate accountCode relationship loanTerm
986985874 2020-02-045-11:00 NA Main account 120
you may use:你可以使用:
# s is your json, you can read from file
pd.DataFrame(s["loanAccount"]["openAccount"])
output:输出:
if you want also to have the other json keys as columns you may use:如果您还想将其他 json 键作为列,您可以使用:
pd.DataFrame([{"loanAccount": '', "openAccount": '', **s["loanAccount"]["openAccount"][0]}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.