用Python从csv数据中创建有序对列表

2024-04-19 01:01:16 发布

您现在位置:Python中文网/ 问答频道 /正文

请在此处引用数据:https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv

数据的最后两列表示[X,Y]坐标对。我想把这些数据组织成一个python中成对列表的列表,这样它看起来像:

coords = [[1486,732],[716,1357],...,[X_ii, Y_ii]]

到目前为止,我已经尝试过:

  nodelist = pd.read_csv('https://gist.githubusercontent.com/brooksandrew/f989e10af17fb4c85b11409fea47895b/raw/a3a8da0fa5b094f1ca9d82e1642b384889ae16e8/nodelist_sleeping_giant.csv')
  coord_array = pd.DataFrame(nodelist.iloc[:,1:].iterrows())
  coords = [[tuple(x)] for x in coord_array.values][0]

但这会产生:

[[[(0, X    1486
    Y     732
    Name: 0, dtype: int64)], [(1, X     716
    Y    1357
    Name: 1, dtype: int64)], [(2, X    3164
    Y    1111
...

Tags: csv数据httpscom列表rawcoordsgist