[英]how to convert a string to a list in a dataframe column on python?
I have a df with the vertices coordinates of polygons.我有一个带有多边形顶点坐标的 df。 I imported that file and the coordinates are strings.
我导入了那个文件,坐标是字符串。 How can I convert those to lists?
如何将它们转换为列表? So the "writing" should be the same but instead of being a string it should be a list of points.
所以“写作”应该是相同的,但不是一个字符串,它应该是一个点列表。
import pandas as pd
df = pd.DataFrame()
data = {'Name': ['B1000', 'B1001', 'B1002'], 'Coordinates': ['[[[-9.124,38.714],[-9.124,38.714],[-9.1231966,38.713],[-9.122,38.7136]]]', '[[[-9.122,38.714],[-9.123,38.713],[-9.1231966,38.713],[-9.122,38.7136]]]', '[[[-9.165,38.740],[-9.165,38.740],[-9.1231966,38.713],[-9.1222,38.7136]]]']}
df = pd.DataFrame(data)
Name coordinates
0 B1000 [[[-9.124,38.714],[-9.124,38.714],[-9.1231966,38.713],[-9.122,38.7136]]]
1 B1001 [[[-9.122,38.714],[-9.123,38.713],[-9.1231966,38.713],[-9.122,38.7136]]]
2 B1002 [[[-9.165,38.740],[-9.165,38.740],[-9.1231966,38.713],[-9.1222,38.7136]]]
# now I have this:
type(df['Coordinates'][0])
str
Alternatively, you could use eval function without using additional modules:或者,您可以在不使用其他模块的情况下使用 eval function:
df['Coordinates'] = df['Coordinates'].apply(lambda s: eval(s))
type(df['coordinates'][0])
list
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.