如何根据CSV数据集填充ShapeFile的位置?

发布于 2025-01-21 16:55:15 字数 544 浏览 1 评论 0原文

我正在使用Python的Geopandas从给定的CSV数据集和佛罗里达州的Shapefile创建佛罗里达州的热图:

“在此处输入映像”

这是我从ShapeFile显示状态的代码,并且CSV数据集内容(这是佛罗里达县的共同案例列表),

Shapefile还方便地具有县名称以及各自多边形的数据:

”

我的计划是通过分析CSV并跟踪每个县有多少个案件,然后从中构建一个热图,但我不确定如何与ShapeFiles一起使用。

I'm using GeoPandas in Python to create a heatmap of the state of Florida from a given CSV dataset and a shapefile of Florida:

enter image description here

This is the code I have for displaying the state from the shapefile, and the CSV dataset contents (It's a list of Covid cases in the Florida counties),

The shapefile also conveniently has data for the the name of the counties along with their respective polygons:

enter image description here

My plan is to parse through the CSV and keep track of how many cases there are for each county then build a heatmap from that, but I'm unsure of how to work with shapefiles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2025-01-28 16:55:15
  • 我相信我已经找到了您正在使用的相同形状文件。我不知道您的covid数据的来源,因此使用了ny times数据( https:// https:// github.com/nytimes/covid-19-data
  • 基于地图的热图的正常术语是
  • 唱片
    • COVID数据由县,几何形状划分为子县。因此,已经将几何形状滚动到县,以使其保持一致
    • 几何形状在两列中编码FIPS,并创建了一个与其组合的新列。 COVID数据具有FIPS作为浮点。已将其修改为字符串
    • 现在,这是 pandas的简单情况 merge()结合/加入几何和covid数据
  • 最终生成视觉效果。这是产生唱片的简单案例。 https://geopandas.org/en/stable/stable/docs/docs/docs/user_guide/mmapping。 html
import geopandas as gpd
import pandas as pd

gdf = gpd.read_file(
    "https://www2.census.gov/geo/tiger/TIGER2016/COUSUB/tl_2016_12_cousub.zip"
)

# NY Times data is by county not sub-county.  rollup geometry to county
gdf_county = (
    gdf.dissolve("COUNTYFP")
    .reset_index()
    .assign(fips=lambda d: d["STATEFP"] + d["COUNTYFP"])
    .loc[:, ["fips", "geometry", "STATEFP", "COUNTYFP", "NAME"]]
)

# get NY times data by county
df = pd.read_csv(
    "https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-counties.csv"
)
# limit to florida and make fips same type as geometry
df_fl = (
    df.loc[df["state"].eq("Florida")]
    .dropna(subset=["fips"])
    .assign(fips=lambda d: d["fips"].astype(int).astype(str))
)

# merge geometry and covid data
gdf_fl_covid = gdf_county.merge(df_fl, on="fips")

# interactive folium choropleth
gdf_fl_covid.explore(column="cases")
# static matplotlib choropleth
gdf_fl_covid.plot(column="cases")

“在此处输入图像描述”

  • I believe I have found same shape file you are working with. I don't know the source of your COVID data so have used NY Times data (https://github.com/nytimes/covid-19-data)
  • the normal terminology for a map based heatmap is a choropleth
  • it's a case of lining up your geometry with your COVID data
    • COVID data is by county, geometry by sub-county. Hence have rolled geometry up to county to make it consistent
    • geometry encodes FIPS in two columns, have created a new column with it combined. COVID data has FIPS as a float. Have modified this to be a string
    • now it's a simple case of a pandas merge() to combine / join geometry and COVID data
  • finally generating the visual. This is a simple case of generating a choropleth. https://geopandas.org/en/stable/docs/user_guide/mapping.html
import geopandas as gpd
import pandas as pd

gdf = gpd.read_file(
    "https://www2.census.gov/geo/tiger/TIGER2016/COUSUB/tl_2016_12_cousub.zip"
)

# NY Times data is by county not sub-county.  rollup geometry to county
gdf_county = (
    gdf.dissolve("COUNTYFP")
    .reset_index()
    .assign(fips=lambda d: d["STATEFP"] + d["COUNTYFP"])
    .loc[:, ["fips", "geometry", "STATEFP", "COUNTYFP", "NAME"]]
)

# get NY times data by county
df = pd.read_csv(
    "https://raw.githubusercontent.com/nytimes/covid-19-data/master/live/us-counties.csv"
)
# limit to florida and make fips same type as geometry
df_fl = (
    df.loc[df["state"].eq("Florida")]
    .dropna(subset=["fips"])
    .assign(fips=lambda d: d["fips"].astype(int).astype(str))
)

# merge geometry and covid data
gdf_fl_covid = gdf_county.merge(df_fl, on="fips")

# interactive folium choropleth
gdf_fl_covid.explore(column="cases")
# static matplotlib choropleth
gdf_fl_covid.plot(column="cases")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文