将 geopandas 行复制到空白 geopandas 行

copy geopandas rows to a blank geopandas row

我有一个要从 geopandas 数据框中提取的邮政编码列表。我能够成功地检测到带有邮政编码列表 (rfp) 的行,但是无法创建新的 geopandas DataFrame,只复制新行。

我尝试追加,iloc

是否有更简单的方法来完成此操作?

i =0
while i < len(rfp):
    for index, row in countries.iterrows():
            if rfp[i] == row['CFSAUID']:
                #print(index, row)
                print(index, row['CFSAUID'], countries.iloc[index])
                #data=data.append[countries.iloc[index]] 
    #print(rfp[i])
    i = i + 1
data.head()

  • 您似乎无缘无故地使 pandas / geopandas 过滤器功能过于复杂
  • 已获取所有美国邮政编码几何图形
  • 使用 loc[]
  • 过滤到 20 个代码
  • 已将几何简化为 质心 以便能够在此答案中显示输出
import geopandas as gpd
gdf = gpd.read_file("https://www2.census.gov/geo/tiger/TIGER2021/ZCTA520/tl_2021_us_zcta520.zip")

zipcode = ['41714', '47994', '40502', '46550', '46385', '46731', '46170',
       '40071', '46049', '46985', '46256', '41603', '40511', '41005',
       '46808', '41006', '47338', '46118', '40003', '41179']


gdf2 = gdf.loc[gdf["ZCTA5CE20"].isin(zipcode)].copy()
gdf2["geometry"] = gdf2["geometry"].centroid
gdf2
ZCTA5CE20 GEOID20 CLASSFP20 MTFCC20 FUNCSTAT20 ALAND20 AWATER20 INTPTLAT20 INTPTLON20 geometry
17 46118 46118 B5 G6350 S 141161691 408941 39.6587 -86.5313 POINT (-86.52394789855478 39.65319687881211)
19 46256 46256 B5 G6350 S 28865547 2051373 39.9084 -86.0131 POINT (-86.0114959356422 39.90683668248156)
36 46731 46731 B5 G6350 S 40833756 74360 40.7935 -85.0996 POINT (-85.10270347937339 40.79753662311887)
66 46985 46985 B5 G6350 S 170789205 115396 40.9595 -86.5805 POINT (-86.58222146640128 40.95485185925257)
67 47994 47994 B5 G6350 S 56334635 0 40.1823 -87.0823 POINT (-87.0823111593409 40.18233088778645)
75 40071 40071 B5 G6350 S 314120461 3703551 38.0429 -85.3788 POINT (-85.37070453567415 38.04328301537532)
76 41005 41005 B5 G6350 S 135617739 97008 39.0068 -84.7601 POINT (-84.7653976667778 39.00706909605251)
81 40511 40511 B5 G6350 S 222838540 808726 38.141 -84.4616 POINT (-84.47394279725246 38.13277356826288)
83 40502 40502 B5 G6350 S 18509366 699624 38.011 -84.4832 POINT (-84.48188158114969 38.01371604197583)
86 40003 40003 B5 G6350 S 112166630 1151993 38.2767 -85.0438 POINT (-85.04368804019533 38.26863127746849)
92 41006 41006 B5 G6350 S 143721210 2283721 38.7844 -84.3456 POINT (-84.34296186884575 38.78473023614392)
100 41179 41179 B5 G6350 S 877946552 2573419 38.535 -83.3701 POINT (-83.38987423897763 38.52484654452977)
108 41603 41603 B5 G6350 S 36349511 116254 37.5715 -82.7046 POINT (-82.68704929399041 37.55933118794476)
140 41714 41714 B5 G6350 S 31218191 51291 37.1632 -83.5058 POINT (-83.50751835227443 37.16518559160255)
142 46385 46385 B5 G6350 S 209128553 1458064 41.4644 -87.1241 POINT (-87.12395316038518 41.46394402403933)
146 46550 46550 B5 G6350 S 185523489 140699 41.4469 -86.0006 POINT (-86.00026862329979 41.4470028890462)
156 46049 46049 B5 G6350 S 67941709 6187 40.295 -86.2218 POINT (-86.22160670624778 40.29760037111507)
163 46808 46808 B5 G6350 S 27442493 192647 41.0978 -85.1771 POINT (-85.17619884243086 41.0988556047745)
176 47338 47338 B5 G6350 S 77579648 653392 40.3424 -85.3374 POINT (-85.33974232857304 40.34362126071514)
188 46170 46170 B5 G6350 S 521838 0 39.5716 -86.8627 POINT (-86.86265114552866 39.57162423401216)