我试图通过他们的苏打API过滤通过纽约政府开放的数据库。我正在按照文档中的说明进行过滤,但是它返回了一个空的数据帧。
# noinspection PyUnresolvedReferences
import numpy as np
# noinspection PyUnresolvedReferences
import pandas as pd
# noinspection PyUnresolvedReferences
from sodapy import Socrata
clientNYgov = Socrata('data.ny.gov', None)这就是我试图只在纽约找到结果的地方。
databaseM = clientNYgov.get('yg7h-zjbf.csv?business_city=NEW+YORK')
dfDatabaseM = pd.DataFrame.from_records(databaseM)
dfDatabaseM.to_csv('Manhattan Agents.csv')
print(dfDatabaseM)但下面是空输出:
0 1 ... 9 10
0 business_address_1 business_address_2 ... license_number license_type
[1 rows x 11 columns]
Process finished with exit code 0如果我的过滤方式有问题,请让我知道,我不太确定这里出了什么问题。提前谢谢你!
发布于 2021-09-11 01:43:56
有两种方法可以通过过滤器来实现这一点。
方法1
这可以使用Socrata()完成,方法是将filters using SQL传递给实例化的Socrata客户机的get()方法中的query关键字。您将需要一个application token。如果您不使用令牌,那么您的请求将是subjected to throttling。要避免限制,请使用sign up for a socrata account和create your app token
query = f"""SELECT * WHERE business_city="NEW YORK" LIMIT 50000"""
client = Socrata("data.ny.gov", <YOUR-APP-TOKEN-HERE>)
results = client.get("yg7h-zjbf", query=query)
df_socrata = pd.DataFrame.from_records(results)方法2
使用JSON端点(与@Joseph Gattuso的answer相同)
data = requests.get(
"http://data.ny.gov/resource/yg7h-zjbf.json?"
"$limit=50000&"
"business_city=NEW YORK"
).json()
df = pd.DataFrame.from_records(data)output的比较-验证两种方法返回的结果是否相同
assert df_socrata.equals(df)发布于 2019-10-04 03:18:40
Socrata使用json端点通过API导出文件。当选择API时,它位于数据集的右上角。在这个解决方案中,我只使用请求来检索数据。Soda模块使用起来很好,但它的工作方式与请求相同。
import pandas as pd
import requests
data=requests.get('http://data.ny.gov/resource/yg7h-zjbf.json?$limit=50000&business_city=NEW YORK').json()
df=pd.DataFrame.from_records(data)
dfhttps://stackoverflow.com/questions/54775015
复制相似问题