-1

我有一个 csv,其中包含伦敦地铁站的名称和纬度/经度位置信息。它看起来像这样:

Station Lat Lng
Abbey Road  51.53195199 0.003737786
Abbey Wood  51.49078408 0.120286371
Acton   51.51688696 -0.267675543
Acton Central   51.50875781 -0.263415792
Acton Town  51.50307148 -0.280288296

我希望转换此 csv 以创建这些站的所有可能组合的原始目的地矩阵。有 270 个站点,因此有 72,900 种可能的组合。

最终我希望将此矩阵转换为具有以下格式的csv

O_Station   O_lat   O_lng   D_Station   D_lat   D_lng
Abbey Road  51.53195199 0.003737786 Abbey Wood  51.49078408 0.120286371
Abbey Road  51.53195199 0.003737786 Acton   51.51688696 -0.267675543
Abbey Road  51.53195199 0.003737786 Acton Central   51.50875781 -0.263415792
Abbey Wood  51.49078408 0.120286371 Abbey Road  51.53195199 0.003737786
Abbey Wood  51.49078408 0.120286371 Acton   51.51688696 -0.267675543
Abbey Wood  51.49078408 0.120286371 Acton Central   51.50875781 -0.263415792
Acton   51.51688696 -0.267675543    Abbey Road  51.53195199 0.003737786
Acton   51.51688696 -0.267675543    Abbey Wood  51.49078408 0.120286371
Acton   51.51688696 -0.267675543    Acton Central   51.50875781 -0.263415792

第一步是将使用循环的任何站点与所有其他可能的站点配对。然后,我需要删除起点和终点是同一站的 0 个组合。

我试过使用 NumPy 函数 column_stack。然而,这给出了一个奇怪的结果。

import csv
import numpy
from pprint import pprint
numpy.set_printoptions(threshold='nan')

with open('./London stations.csv', 'rU') as csvfile:
    reader = csv.DictReader(csvfile)
    Stations = ['{O_Station}'.format(**row) for row in reader]
print(Stations)
O_D = numpy.column_stack(([Stations],[Stations]))
pprint(O_D)

输出

车站 =

['Abbey Road', 'Abbey Wood', 'Acton', 'Acton Central', 'Acton Town']

O_D =

array([['Abbey Road', 'Abbey Wood', 'Acton', 'Acton Central', 'Acton Town',
        'Abbey Road', 'Abbey Wood', 'Acton', 'Acton Central', 'Acton Town']], 
      dtype='|S13')

理想情况下,我正在寻找更合适的功能,并且很难在 Numpy 手册中找到它。

4

2 回答 2

0

在处理这样的表格数据时,我更喜欢使用 pandas。它使控制您的数据结构变得简单。

import pandas as pd

#read in csv
stations = pd.read_csv('london stations.csv', index_col = 0)

#create new dataframe
O_D = pd.DataFrame(columns = ['O_Station','O_lat','O_lng','D_Station','D_lat','D_lng'])

#iterate through the stations

new_index= 0
for o_station in stations.index:
    for d_station in stations.index:
        ls = [o_station,stations.Lat.loc[o_station],stations.Lng.loc[o_station],d_station, stations.Lat.loc[d_station], stations.Lng.loc[d_station]]
        O_D.loc[new_index] = ls
        new_index+=1

#remove double stations
O_D = O_D[O_D.O_Station != O_D.D_Station]

这应该可以解决您的数据转换问题。

于 2015-08-19T18:49:49.830 回答
0

这是一个不完整的答案,但我会跳过 numpy 并直接进入pandas

csv_file = '''Station Lat Lng
Abbey Road  51.53195199 0.003737786
Abbey Wood  51.49078408 0.120286371
Acton   51.51688696 -0.267675543
Acton Central   51.50875781 -0.263415792
Acton Town  51.50307148 -0.280288296'''

这很难,因为它不是真正的逗号分隔,否则我们可以调用pandas.read_csv()

names = [' '.join(x.split()[:-2]) for x in stations]
lats = [x.split()[-2] for x in stations]
lons = [x.split()[-1] for x in stations]

stations_dict = {names[i]: (lats[i], lons[i]) for i, _ in enumerate(stations)}

df = pd.DataFrame(stations_dict).T    # Transpose it
df.columns = ['Lat', 'Lng']
df.index.name = 'Station'

所以我们最终df.head()得到了:

                       Lat           Lng
Station
Abbey Road     51.53195199   0.003737786
Abbey Wood     51.49078408   0.120286371
Acton          51.51688696  -0.267675543
Acton Central  51.50875781  -0.263415792
Acton Town     51.50307148  -0.280288296

获得排列可能意味着我们不需要将站点作为索引......目前不确定。希望这会有所帮助!

于 2015-08-19T17:45:03.047 回答