Python 如何将json文件规范化为数据帧?
我有一个json文件,如下所示。我已经试过这个解决方案了 ] 当我尝试使用pandas读取Python 如何将json文件规范化为数据帧?,python,json,pandas,dataframe,readfile,Python,Json,Pandas,Dataframe,Readfile,我有一个json文件,如下所示。我已经试过这个解决方案了 ] 当我尝试使用pandas读取json文件时,出现以下错误 ValueError:预期的对象或值 我已经尝试了以下方法 df = pd.read_json(open(r"test_data.json", "r",encoding="utf8")) df = pd.read_json(r'test_data.json', encoding='utf-8-sig') basepat
json
文件时,出现以下错误
ValueError:预期的对象或值
我已经尝试了以下方法
df = pd.read_json(open(r"test_data.json", "r",encoding="utf8"))
df = pd.read_json(r'test_data.json', encoding='utf-8-sig')
basepath = 'C:\\Users\\test\\Downloads'
pd.read_json(basePath + '\\test_data.json')
我希望我的输出如下所示
错误表明它不理解代码的一部分 您应该检查代码的格式。要做到这一点的页面是:
错误表明它不理解代码的一部分 您应该检查代码的格式。要做到这一点的页面是: 用于展平嵌套数据 代码
# Load json data from file
with open('test.json') as f:
adict = json.load(f)
# Use json normalize for nested dictionaries
df = pd.json_normalize(adict)
df
answersData.employeeId answersData.answers.Address_2 answersData.answers.Address_2_CC answersData.answers.CellphoneNumberConsent answersData.answers.Consent_Given answersData.answers.DoB answersData.answers.EmailaddressConsent answersData.answers.First_Name answersData.answers.Gender answersData.answers.Last_Name ... answersData.answers.countryName answersData.answers.householdResponsibility answersData.answers.poorCardHas answersData.answers.poorCardReason answersData.answers.postalCode answersData.answers.profilePicture answersData.answers.provinceCity answersData.createdBy answersData.dateCreated answersData.type
0 0923a Address_Line_2_1 Address_2_CC_1 YES Y 1971-07-10T16:00:00.000Z NotApplicable First_Name_1 M Last_Name_1 ... IND husband N OTH 123456 None Province_1 MAM_1@123.com 2021-02-23T17:20:33.134Z profile
1 27l23t Address_Line_2_2 Address_2_CC_2 YES Y 1980-07-10T16:00:00.000Z NotApplicable First_Name_2 M Last_Name_2 ... IND wife N OTH 621 None Province_2 MAM_2@123.com 2021-02-23T17:20:33.134Z profile
2 290p Address_Line_2_3 Address_2_CC_3 NO N 1991-10-10T16:00:00.000Z NotApplicable First_Name_3 M Last_Name_3 ... IND Father N OTH 123456 None Province_3 MAM_3@123.com 2021-01-11T19:11:20.134Z profile
3 17mk9i Address_Line_2_4 Address_2_CC_4 YES Y 1947-07-10T16:00:00.000Z NotApplicable First_Name_4 M Last_Name_4 ... IND mother N OTH 123456 None Province_4 MAM_4@123.com 2021-05-23T17:20:33.134Z profile
4 17lo8i Address_Line_2_5 Address_2_CC_5 YES Y 1993-07-10T16:00:00.000Z NotApplicable First_Name_5 M Last_Name_5 ... IND child N OTH 123456 None Province_5 MAM_5@123.com 2021-01-01T17:20:33.134Z profile
5 17k9i Address_Line_2_6 Address_2_CC_6 YES Y 1983-07-10T16:00:00.000Z NotApplicable First_Name_6 M Last_Name_6 ... IND husband N OTH 123456 None Province_6 MAM_6@123.com 2021-01-16T17:20:33.134Z profile
6 87p TEST123 Test YES Y 1801-07-10T16:00:00.000Z NotApplicable Test123 M Test123 ... IND wife N OTH 654321 None Province_2 jo@test.com 2021-02-23T17:20:33.134Z profile
7 09l07ytw TEST123 Test YES Y 1801-07-10T16:00:00.000Z NotApplicable Test123 M Test123 ... IND wife N OTH 654321 None Province_2 jo@test.com 2021-02-23T17:20:33.134Z profile
用于展平嵌套数据
代码
# Load json data from file
with open('test.json') as f:
adict = json.load(f)
# Use json normalize for nested dictionaries
df = pd.json_normalize(adict)
df
answersData.employeeId answersData.answers.Address_2 answersData.answers.Address_2_CC answersData.answers.CellphoneNumberConsent answersData.answers.Consent_Given answersData.answers.DoB answersData.answers.EmailaddressConsent answersData.answers.First_Name answersData.answers.Gender answersData.answers.Last_Name ... answersData.answers.countryName answersData.answers.householdResponsibility answersData.answers.poorCardHas answersData.answers.poorCardReason answersData.answers.postalCode answersData.answers.profilePicture answersData.answers.provinceCity answersData.createdBy answersData.dateCreated answersData.type
0 0923a Address_Line_2_1 Address_2_CC_1 YES Y 1971-07-10T16:00:00.000Z NotApplicable First_Name_1 M Last_Name_1 ... IND husband N OTH 123456 None Province_1 MAM_1@123.com 2021-02-23T17:20:33.134Z profile
1 27l23t Address_Line_2_2 Address_2_CC_2 YES Y 1980-07-10T16:00:00.000Z NotApplicable First_Name_2 M Last_Name_2 ... IND wife N OTH 621 None Province_2 MAM_2@123.com 2021-02-23T17:20:33.134Z profile
2 290p Address_Line_2_3 Address_2_CC_3 NO N 1991-10-10T16:00:00.000Z NotApplicable First_Name_3 M Last_Name_3 ... IND Father N OTH 123456 None Province_3 MAM_3@123.com 2021-01-11T19:11:20.134Z profile
3 17mk9i Address_Line_2_4 Address_2_CC_4 YES Y 1947-07-10T16:00:00.000Z NotApplicable First_Name_4 M Last_Name_4 ... IND mother N OTH 123456 None Province_4 MAM_4@123.com 2021-05-23T17:20:33.134Z profile
4 17lo8i Address_Line_2_5 Address_2_CC_5 YES Y 1993-07-10T16:00:00.000Z NotApplicable First_Name_5 M Last_Name_5 ... IND child N OTH 123456 None Province_5 MAM_5@123.com 2021-01-01T17:20:33.134Z profile
5 17k9i Address_Line_2_6 Address_2_CC_6 YES Y 1983-07-10T16:00:00.000Z NotApplicable First_Name_6 M Last_Name_6 ... IND husband N OTH 123456 None Province_6 MAM_6@123.com 2021-01-16T17:20:33.134Z profile
6 87p TEST123 Test YES Y 1801-07-10T16:00:00.000Z NotApplicable Test123 M Test123 ... IND wife N OTH 654321 None Province_2 jo@test.com 2021-02-23T17:20:33.134Z profile
7 09l07ytw TEST123 Test YES Y 1801-07-10T16:00:00.000Z NotApplicable Test123 M Test123 ... IND wife N OTH 654321 None Province_2 jo@test.com 2021-02-23T17:20:33.134Z profile
这不是有效的JSON,在
}{
搜索字符串文字}{
之间缺少一个,
-只有一个。它在列表中最后一个对象之前。@TheGreat You can's use:即使在我修复之后,
之间的问题{,我仍然会得到错误ValueError:Expected object或value
@DavidMeu-在修复,
问题后,检查格式化程序说它是有效的valid(RFC 8259)
这不是有效的JSON,在}之间缺少,
搜索字符串文字}{
-只有一个。它在列表中最后一个对象之前。@TheGreat您可以使用:即使我修复了,
之间的问题{
,我仍然会得到错误值错误:预期的对象或值
@DavidMeu-在修复,
问题后,并且签入格式化程序后说它是有效的有效的(RFC 8259)
谢谢,向上投票。但我已经修复了格式问题缺少逗号
,但它仍然会抛出错误,lines=True
表示“将文件作为每行的json对象读取”。因此您的值错误:预期对象或值
“数据帧格式”-然后我建议你展示一下你希望数据帧的样子。当然,你没想到我们会猜到吗?我在帖子中更新了当前的输出。谢谢,投了赞成票。但是我已经修复了格式问题缺少逗号
,但它仍然抛出错误,lines=True
意味着“每行将文件作为json对象读取”因此,您的ValueError:Expected object或value
“数据帧格式”-然后我建议你展示一下你想要的数据帧的样子。当然,你没想到我们会猜?我在帖子中更新了当前的输出。哇,太棒了。谢谢,upvoted@TheGreat--很高兴我能帮上忙。这篇文章展示了如何获得不同程度的扁平化。哇,太棒了。谢谢,upvoted@TheGreat--很高兴我能帮忙。文章显示如何获得不同程度的扁平化。