【问题标题】:How to save and load any python variable directly to/from excel?如何将任何python变量直接保存到/从excel中加载?
【发布时间】:2020-12-01 03:16:19
【问题描述】:

有没有办法将任何类型的 python 变量从数据框中保存到 excel 中,然后能够在需要时在 python 中加载该变量。 以下图为例,我正在使用 shapely,以下是我保存在 excel 中的一些数据,是否可以将圆形边界列直接作为 shapely 变量而不是字符串加载到 python 中?

【问题讨论】:

标签: python excel pandas dataframe shapely


【解决方案1】:

您的专栏采用称为众所周知的文本或 WKT 的格式。您可以使用 shapely.wkt.loads() 从字符串转换为 Shapely 几何。

首先将 Excel 作为 DataFrame 加载:

import pandas

df = pandas.read_excel('path/to/my/file.xlsx')

接下来,从字符串中加载几何图形:

from shapely import wkt

df['Circular Boundary'] = df['Circular Boundary'].apply(wkt.loads)

如果您想对几何做更多的工作,您可能希望使用新加载的几何转换为 geopandas GeoDataFrame:

import geopandas

gdf = geopandas.GeoDataFrame(df, geometry='Circular Boundary')

这都是基于geopandas docs中的一个例子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 2019-06-12
    • 1970-01-01
    • 2015-12-30
    • 2015-08-20
    • 2016-11-04
    相关资源
    最近更新 更多