首页 > 解决方案 > 如何将数据正确保存到列表中,然后在 python 中将其转换为 DataFrame?

问题描述

我有包含有关建筑物(Building_Number,几何)信息的 GeoDataFrame,我需要从中创建另一个 GeoDataFrame - 包含有关具有相同列(Building_Number,几何)的建筑物中心点(质心)的信息。

我的想法是首先创建一个空列表并将结果附加到列表中:

    import geopadnas as gpd
    result = []
    centroids = gpd.GeoSeries(My_GDF.centroid)
    result.append([My_GDF['Buliding_Number'],centroids])
    print(result)

然而这对我不起作用,输出是:

[[0     61
1      5
2     22
3    NaN
4      7
5    NaN
6    NaN
7     5D
Name: BUI_NUMBER, dtype: object, 0    POINT (21.04572 52.14893)
1    POINT (21.02783 52.07371)
2    POINT (20.97048 52.22921)
3    POINT (20.93536 52.25316)
4    POINT (20.93575 52.25281)
5    POINT (20.93293 52.24256)
6    POINT (20.93444 52.24029)
7    POINT (20.93574 52.25361)
dtype: geometry]]

我想我需要这样的输出:

[[61, 5, 22, NaN, 7, NaN, NaN, 5D],[POINT (21.02783 52.07371, ..., POINT (20.93574 52.25361)]]

因为我想将它转换为 GeoDataFrame(这个,我知道该怎么做)。有什么方法可以按照我想要的方式将数据附加到列表中?或者,也许我可以将它从我现在得到的输出转换为 GDF?总结一下,我的想法是:

标签: pythonlistdataframeappendcentroid

解决方案


像这样的东西对您创建新的 GeoDataFrame 有用吗?(顺便说一句,你在“bulding”键中有一个类型)

df = pandas.DataFrame(
  {
    'BuildingID': My_GDF['Buliding_Number']
  #, centroids however those are put
  }
)

gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))

在https://repl.it/@ToniAlatalo/GeoDataFrame中探索这个


推荐阅读