如何使用来自 JSON 输出的数据创建数据框
How to create a dataframe with data from JSON output
我使用网络 API 从特定网站导入数据。我能够以 JSON 格式导入数据。我是 python 的新手,因此很难将其转换为表格格式,我可以将其用于数据分析。这是我的示例代码;
import json
import ast
import pandas as pd
import requests
from pandas import json_normalize
result = requests.get('https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=dfgdfgdfgdsfgdfgd&limit=200') << #Open Source
data = result.json()
示例 JSOn 输出;
{'help': 'https://discover.data.vic.gov.au/api/3/action/help_show?name=datastore_search',
'success': True,
'result': {'include_total': True,
'resource_id': 'bc71e010-253a-482a-bdbc-d65d1befe526',
'fields': [{'type': 'int', 'id': '_id'},
{'type': 'text', 'id': 'LGA'},
{'type': 'text', 'id': 'lga_pid'},
{'type': 'text', 'id': 'population'},
{'type': 'text', 'id': 'active'},
{'type': 'text', 'id': 'cases'},
{'type': 'text', 'id': 'rate'},
{'type': 'text', 'id': 'new'},
{'type': 'text', 'id': 'band'},
{'type': 'text', 'id': 'LGADisplay'},
{'type': 'text', 'id': 'data_date'},
{'type': 'text', 'id': 'file_processed_date'}],
'records_format': 'objects',
'records': [{'_id': 1,
'LGA': 'Alpine (S)',
'lga_pid': 'VIC242',
'population': '12814',
'active': '0',
'cases': '1',
'rate': '0',
'new': '0',
'band': '0',
'LGADisplay': 'Alpine',
'data_date': '14/06/2021',
'file_processed_date': '15/06/2021'},
实际输出的个数_id = "81"
尝试使用此功能;
pd.json_normalize(data, record_path=['result'])
无法将它们转换为表格格式,需要输出;
如有任何帮助,我们将不胜感激!!
是否如你所愿?
>>> pd.DataFrame(data['result']['records'])
_id LGA lga_pid ... LGADisplay data_date file_processed_date
0 1 Alpine (S) VIC242 ... Alpine 14/06/2021 15/06/2021
1 2 Ararat (RC) VIC220 ... Ararat 14/06/2021 15/06/2021
2 3 Ballarat (C) VIC241 ... Ballarat 14/06/2021 15/06/2021
3 4 Banyule (C) VIC188 ... Banyule 14/06/2021 15/06/2021
4 5 Bass Coast (S) VIC173 ... Bass Coast 14/06/2021 15/06/2021
.. ... ... ... ... ... ... ...
76 77 Wodonga (RC) VIC192 ... Wodonga 14/06/2021 15/06/2021
77 78 Wyndham (C) VIC221 ... Wyndham 14/06/2021 15/06/2021
78 79 Yarra (C) VIC202 ... Yarra 14/06/2021 15/06/2021
79 80 Yarra Ranges (S) VIC206 ... Yarra Ranges 14/06/2021 15/06/2021
80 81 Yarriambiack (S) VIC200 ... Yarriambiack 14/06/2021 15/06/2021
[81 rows x 12 columns]
我使用网络 API 从特定网站导入数据。我能够以 JSON 格式导入数据。我是 python 的新手,因此很难将其转换为表格格式,我可以将其用于数据分析。这是我的示例代码;
import json
import ast
import pandas as pd
import requests
from pandas import json_normalize
result = requests.get('https://discover.data.vic.gov.au/api/3/action/datastore_search?resource_id=dfgdfgdfgdsfgdfgd&limit=200') << #Open Source
data = result.json()
示例 JSOn 输出;
{'help': 'https://discover.data.vic.gov.au/api/3/action/help_show?name=datastore_search',
'success': True,
'result': {'include_total': True,
'resource_id': 'bc71e010-253a-482a-bdbc-d65d1befe526',
'fields': [{'type': 'int', 'id': '_id'},
{'type': 'text', 'id': 'LGA'},
{'type': 'text', 'id': 'lga_pid'},
{'type': 'text', 'id': 'population'},
{'type': 'text', 'id': 'active'},
{'type': 'text', 'id': 'cases'},
{'type': 'text', 'id': 'rate'},
{'type': 'text', 'id': 'new'},
{'type': 'text', 'id': 'band'},
{'type': 'text', 'id': 'LGADisplay'},
{'type': 'text', 'id': 'data_date'},
{'type': 'text', 'id': 'file_processed_date'}],
'records_format': 'objects',
'records': [{'_id': 1,
'LGA': 'Alpine (S)',
'lga_pid': 'VIC242',
'population': '12814',
'active': '0',
'cases': '1',
'rate': '0',
'new': '0',
'band': '0',
'LGADisplay': 'Alpine',
'data_date': '14/06/2021',
'file_processed_date': '15/06/2021'},
实际输出的个数_id = "81"
尝试使用此功能;
pd.json_normalize(data, record_path=['result'])
无法将它们转换为表格格式,需要输出;
如有任何帮助,我们将不胜感激!!
是否如你所愿?
>>> pd.DataFrame(data['result']['records'])
_id LGA lga_pid ... LGADisplay data_date file_processed_date
0 1 Alpine (S) VIC242 ... Alpine 14/06/2021 15/06/2021
1 2 Ararat (RC) VIC220 ... Ararat 14/06/2021 15/06/2021
2 3 Ballarat (C) VIC241 ... Ballarat 14/06/2021 15/06/2021
3 4 Banyule (C) VIC188 ... Banyule 14/06/2021 15/06/2021
4 5 Bass Coast (S) VIC173 ... Bass Coast 14/06/2021 15/06/2021
.. ... ... ... ... ... ... ...
76 77 Wodonga (RC) VIC192 ... Wodonga 14/06/2021 15/06/2021
77 78 Wyndham (C) VIC221 ... Wyndham 14/06/2021 15/06/2021
78 79 Yarra (C) VIC202 ... Yarra 14/06/2021 15/06/2021
79 80 Yarra Ranges (S) VIC206 ... Yarra Ranges 14/06/2021 15/06/2021
80 81 Yarriambiack (S) VIC200 ... Yarriambiack 14/06/2021 15/06/2021
[81 rows x 12 columns]