的步骤如下:
import requests
import pandas as pd
url = "https://api.usaspending.gov/api/v2/search/spending_by_award/"
# 构建查询条件
query = {
"filters": [
{
"field": "recipient_name",
"operation": "equals",
"value": "ABC Company"
},
{
"field": "date_signed",
"operation": "greater_than",
"value": "2021-01-01"
}
],
"fields": [
"recipient_name",
"date_signed",
"award_amount"
],
"limit": 100
}
# 发送API请求
response = requests.post(url, json=query)
# 解析API响应
data = response.json()
# 提取数据
results = data["results"]
# 转换为DataFrame格式
df = pd.DataFrame(results)
# 排序
df_sorted = df.sort_values(by="award_amount", ascending=False)
# 筛选
df_filtered = df[df["date_signed"] > "2021-06-01"]
# 计算统计指标
total_amount = df["award_amount"].sum()
average_amount = df["award_amount"].mean()
以上是使用Python和usaspending.gov API提交高级过滤查询条件并使用pandas访问数据的基本步骤。根据具体需求,可以进一步扩展和优化代码,例如添加异常处理、数据可视化等功能。
领取专属 10元无门槛券
手把手带您无忧上云