Я работаю над этим упражнением, которое требует от меня:
- Сохранить файл travelTimes_2015_Helsinki.txt на вашем компьютере.
- Прочитать 4 столбца, то есть «from_x», «from_y», 'to_x', 'to_y' из данных в Python с использованием Pandas.
- Создайте два списка с именами orig_points и dest_points
- Переберите строки вашего DataFrame и добавьте объекты Shapely Point в объектыorig_points -list и dest_point -list, представляющие местоположения источника и места назначения соответственно.
Используются эти данные: (https://github.com/AutoGIS-2017/Exercise-1/blob/master/data/travelTimes_2015_Helsinki.txt)
Я загрузил текстовый файл в панды и прочитали приступил к выполнению задачи. Я создал список 'orig_points' и список 'dest_points'. Я также перебрал четыре строки и создал из них фигурные точки. Все, что мне теперь нужно сделать, это добавить эти точки ксоответствующие списки, но у меня возникли проблемы с этим.
import pandas as pd
from shapely.geometry import Point
orig_points = []
dest_points = []
df = pd.read_csv('travelTimes_2015_Helsinki.txt', )
print(df)
print(df.columns)
df1 = df['from_id;to_id;fromid_toid;route_number;at;from_x;from_y;to_x;to_y;total_route_time;route_time;route_distance;route_total_lines'].str.split(';', expand=True)
df1.columns = ['from_id', 'to_id', 'fromid_toid', 'route_number', 'at', 'from_x', 'from_y', 'to_x', 'to_y', 'total_route_time', 'route_time', 'route_distance', 'route_total_lines']
print (df1)
print(df1[['from_x', 'from_y', 'to_x', 'to_y']])
#iterating rows in dataframe
for index, row in df1.iterrows():
print(row['from_x'], row['from_y'], row['to_x'], row['to_y'])
print(df1.dtypes) #checking the data type of the columns and changing the columns I need to float, so that the iteration could be done.
df1['from_x'] = df1['from_x'].astype(float)
df1['from_y'] = df1['from_y'].astype(float)
df1['to_x'] = df1['to_x'].astype(float)
df1['to_y'] = df1['to_y'].astype(float)
print(df1.dtypes)
for index, row in df1.iterrows():
shapely_points1 = (Point(row['from_x'], row['from_y'])) #creating the shapely points from the points in the two columns('from_x' and 'from_y') which are the origin points.
print(shapely_points1)
for index, row in df1.iterrows():
Point(row['to_x'], row['to_y'])
shapely_points2 = (Point(row['to_x'], row['to_y'])) #creating the shapely points from the points in the two columns('to_x' and 'to_y') which are the destination points
print(shapely_points2)
for i in (row['from_x'], row['from_y']):
orig_points.append(Point(i))
print(orig_points)
Я попытался добавить фигурную точку к пустому списку (orig_points), но я получил tTypeError: TypeError: объект 'float' не повторяется
Любая помощь будет по достоинству оценена.Спасибо.