我正在嘗試將此美國人口普查局 api 數(shù)據(jù)獲取請求放入數(shù)據(jù)框中,并認為它是一個列表列表,但顯示為 NoneType。有沒有辦法將其制作成可以輕松導出到 CSV 文件的數(shù)據(jù)框?import request# The Basic API Request:# Build base URLHOST = "https://api.census.gov/data"year = "2010"dataset = "dec/sf1"base_url = "/".join([HOST, year, dataset])# Specify Census variables and other predicatesget_vars = ["NAME","P013001","P037001"]predicates = {}predicates["get"] = ",".join(get_vars)predicates["for"] = "state:*"# Execute the request, examine text of response objectdata = requests.get(base_url, params=predicates)print(data.text)這確實會產(chǎn)生以下輸出:[["NAME","P013001","P037001","state"],["Alabama","37.9","3.02","01"],["Alaska","33.8","3.21","02"],["Arizona","35.9","3.19","04"],...["Wyoming","36.8","2.96","56"],["Puerto Rico","36.9","3.17","72"]]
1 回答

胡說叔叔
TA貢獻1804條經(jīng)驗 獲得超8個贊
這data.text是一個字符串,所以你可以通過json解析它,試試這個
import json
import pandas as pd
data = pd.DataFrame(json.loads(data.text)[1:], columns=['NAME', 'P013001', 'P037001', 'state'])
你會得到類似于下圖的東西。
添加回答
舉報
0/150
提交
取消