提问人:Ashley 提问时间:9/6/2023 最后编辑:mkrieger1Ashley 更新时间:9/6/2023 访问量:52
Python 中的 json - str 到 int
json in Python - str to int
问:
我正在尝试学习如何使用 Python 创建地理可视化。我已经下载了英国城市的json。但是它们是作为 str 进来的,所以我不知道如何使用它们。
我的json来自这里:https://simplemaps.com/data/gb-cities
我的代码是:
# Setup a folium map at a high-level zoom
venue_map = folium.Map(location = [20,78], zoom_start = 2.5)
# Choropleth maps bind Pandas Data Frames and json geometries.This allows us to quickly visualize data combinations
folium.Choropleth(
geo_data = country_geo,
data = data_to_plot1,
columns = ["city", "xg"],
key_on = 'feature.CopyPasteDiv',
fill_color = 'YlOrBr', fill_opacity=0.6, line_opacity=0.1,
legend_name = "Expected Goals per city").add_to(venue_map)
folium.LayerControl().add_to(venue_map)
venue_map
我也想知道我的是否正确。key_on
答:
0赞
JonSG
9/6/2023
#1
如果我从该站点下载文件,我会得到一个文件,该文件是 json 字典的列表,例如:gb.json
[
{
"city": "London",
"lat": "51.5072",
"lng": "-0.1275",
"country": "United Kingdom",
"iso2": "GB",
"admin_name": "London, City of",
"capital": "primary",
"population": "11262000",
"population_proper": "8825001"
}
]
我认为您遇到的问题与字符串而不是数字有关。作为次要问题,我在这些项目中没有看到属性,所以也许你想要一个不同的属性。"population"
"xg"
要将总体数字转换为整数,您可以执行以下操作:
import json
with open("gb.json", "r") as file_in:
rows = json.load(file_in)
for row in rows:
row["population"] = int(row["population"])
print(rows)
然后尝试传递给rows
folium.Choropleth()
评论