我正在尝试将元组从dataframe转换为一行字符串。这是从csv文件导入的数据文件的一部分。
Unnamed: 0 name route decode
0 0 Funshine! ofosF|mqaShJ@?rLh@d@veCIVd@LbEJfJ^f@lE?Rp@^L~g... '[(-105.28, 39.999), (-105.282, 39.998), (-105.282, 39.99), (-105.28, 39.995), (-105.282, 39.99), (etc)]'如果我手动复制解码列的内容并将其粘贴到LineString()条件中,它将对其进行转换。我收到的错误张贴在下面。
line = LineString(df.decode[0])
print(line)Traceback (most recent call last):
File "shapely\speedups\_speedups.pyx", line 86, in shapely.speedups._speedups.geos_linestring_from_py
AttributeError: 'str' object has no attribute '__array_interface__'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/taylo/PycharmProjects/PermitProj/Polyline Decode.py", line 20, in <module>
line = LineString(df.decode[1])
File "C:\Users\taylo\Miniconda3\envs\ExcelPermitExe\lib\site-packages\shapely\geometry\linestring.py", line 48, in __init__
self._set_coords(coordinates)
File "C:\Users\taylo\Miniconda3\envs\ExcelPermitExe\lib\site-packages\shapely\geometry\linestring.py", line 97, in _set_coords
ret = geos_linestring_from_py(coordinates)
File "shapely\speedups\_speedups.pyx", line 166, in shapely.speedups._speedups.geos_linestring_from_py
AssertionError我最终想要循环它,所以我将它设置为dataframe列解码。这是我为最终将行字符串写入列而创建的循环。
def linestringdecode(name, decode):
try:
return LineString(decode)
except:
print(name)
return np.nan
df['decode'] = df.apply(lambda x: linestringdecode(x[1], x[3]), axis=1)如何编写它,以避免此错误,并将元组转换为dataframe中的列?
发布于 2020-04-13 00:24:06
答案在本节中找到。
https://gis.stackexchange.com/questions/358068/converting-to-linestring-using-dataframe-column/
df['decode'] = df.decode.apply(lambda row: LineString(eval(row)))编辑: eval()使用起来很危险。确保您使用的是可信数据。
发布于 2020-04-12 22:55:39
编辑最终解决方案
经过一些清理之后,结果是将列decode保存为字符串"[(1,1),(2,3),(4,4),(1,3)]",该字符串首先需要转换为元组列表。在使用密集列表理解进行转换之后,LineString转换将按以下方式工作
df['decode'] = [eval(ele) for ele in df.decode.str.strip()[:]]
df['decode'] = df.apply(lambda x: linestringdecode(x[1], x[4]), axis=1)Alternative进行此操作的另一个选项是已经修复了导入。通过在ast.literal_eval的帮助下直接将字符串转换为元组列表,如本SO Question中所建议的那样
import ast
df = pd.read_csv("Test_Csv_With_List.csv", quotechar='"', sep=",",converters={4:ast.literal_eval})
编辑前的:--我尝试用下面的代码重现您的错误。然而,它运行完全良好,没有任何错误。
from shapely.geometry import LineString
import pandas as pd
def linestringdecode(name, decode):
try:
return LineString(decode)
except:
print(name)
return np.nan
data = {'Unamed 0': [0,1],
'name': ['test','test2'],
'rote': ['Gibberish','moreGib'],
'decode': [[(-105.27983, 40.06008), (-105.27984, 40.05827)],[(-23, 23), (-22, 24)]]}
df = pd.DataFrame(data)
# print(df)
df['decode'] = df.apply(lambda x: linestringdecode(x[1], x[3]), axis=1)从您的错误消息AttributeError: 'str'中,我想我可以推断您的数据导入有问题。我的假设是,解码具有dtype对象,而不是list。
请验证函数linestringdecode()传递的参数decode是否为list类型,而不是字符串。
https://stackoverflow.com/questions/61177522
复制相似问题