Pandas/Geopandas 与蒙版选择合并

发布于 2025-01-16 08:02:00 字数 665 浏览 2 评论 0原文

我通常使用 Arcpy,但我正在尝试了解更多 pandas/geopandas 的用途。我有一个应用于 csv 表和 shapefile 的掩码,我想将其合并在一起,以便根据特定字段查找两者之间的匹配项。

然而,当我尝试将它们合并在一起时,我收到错误“数据框的真值不明确”。如何合并屏蔽数据框?我在下面添加了创建掩码(利用两个日期变量和一个日期字段)的代码段以及使用位置字段(每个数据帧上的不同名称)的合并。

我需要做什么才能操作掩码数据帧使其在掩码中发挥作用?

    mask = (svc_df['createdate'] < curdate) & (svc_df['createdate'] >= backdate)
    print(svc_df.loc[mask])
    # Detect the sub-dataframe and then assign to a new dataframe
    sel_df = svc_df.loc[mask]
    #Create a geodf from alabama services
    al_gdf = geopandas.read_file(alSvc_shp)
    al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")

I usually work with Arcpy but am trying to learn more pandas/geopandas uses. I have a mask applied to a csv table and a shapefile that I want to merge together in order to find matches between the two based on a specific field.

However, when I try to merge them together, I get the error "The truth value of a Dataframe is ambiguous." How do I merge a masked dataframe? I've included the segment of code below that creates the mask (utilizing two date variables and a date field) and the merge which uses the Location fields (different names on each dataframe).

What do I need to do to manipulate the mask dataframe into functioning in a mask?

    mask = (svc_df['createdate'] < curdate) & (svc_df['createdate'] >= backdate)
    print(svc_df.loc[mask])
    # Detect the sub-dataframe and then assign to a new dataframe
    sel_df = svc_df.loc[mask]
    #Create a geodf from alabama services
    al_gdf = geopandas.read_file(alSvc_shp)
    al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")

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

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

发布评论

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

评论(1

紫南 2025-01-23 08:02:00
  • 已从您的代码合成了 MWE。生成数据框和地理数据框
  • 时出现错误:
al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")
  • 使用了 dataframe.merge() 而不是 pd.merge() 因此只有一个数据框应该作为 下面的参数
  • 完整工作示例
import pandas as pd
import numpy as np
import geopandas as gpd

# synthesize
svc_df = pd.DataFrame(
    {
        "createdate": pd.date_range("1-mar-2022", periods=30),
        "sketch_LOC": np.random.choice(["CHN", "USA", "IND", "JPN", "DEU"], 30),
    }
)
curdate = pd.to_datetime("today")
backdate = curdate - pd.Timedelta("5 days")

mask = (svc_df["createdate"] < curdate) & (svc_df["createdate"] >= backdate)
print(svc_df.loc[mask])
# Detect the sub-dataframe and then assign to a new dataframe
sel_df = svc_df.loc[mask]
# Create a geodf from alabama services
# al_gdf = geopandas.read_file(alSvc_shp)
# synthesize
al_gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).assign(
    Location=lambda d: d["iso_a3"]
)
al_merge = al_gdf.merge(sel_df, left_on="Location", right_on="sketch_LOC")


  • have synthesized a MWE from your code. Generation and data frame and geo data frame
  • you have an error:
al_merge = al_gdf.merge(al_gdf, sel_df, left_on="Location", right_on="sketch_LOC")
  • have used dataframe.merge() not pd.merge() hence only one data frame should be passed as a parameter
  • full working example below
import pandas as pd
import numpy as np
import geopandas as gpd

# synthesize
svc_df = pd.DataFrame(
    {
        "createdate": pd.date_range("1-mar-2022", periods=30),
        "sketch_LOC": np.random.choice(["CHN", "USA", "IND", "JPN", "DEU"], 30),
    }
)
curdate = pd.to_datetime("today")
backdate = curdate - pd.Timedelta("5 days")

mask = (svc_df["createdate"] < curdate) & (svc_df["createdate"] >= backdate)
print(svc_df.loc[mask])
# Detect the sub-dataframe and then assign to a new dataframe
sel_df = svc_df.loc[mask]
# Create a geodf from alabama services
# al_gdf = geopandas.read_file(alSvc_shp)
# synthesize
al_gdf = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")).assign(
    Location=lambda d: d["iso_a3"]
)
al_merge = al_gdf.merge(sel_df, left_on="Location", right_on="sketch_LOC")


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