Python数据框索引作为XY坐标的元组

问题描述 投票:0回答:1

我的功能collectHUC获取XY坐标,并为1000个站建立名为stationsXY的HUC代码字典。 keys是类似于('41.462605 ', '-74.387089')的元组,而类似于values0202000600。此过程需要时间,因此我将stationsXY转换为DataFrame并保存如下,以备后用:

xy = pd.DataFrame.from_dict(stationsXY, orient='index')
xy.to_csv('xy_huc.csv', index=False)

这里是问题,当用[回读文件xy_huc.csv

xy_coor = pd.read_csv('xy_huc.csv', index_col=0, header=None)

DataFrame xy_coor具有2列,通过将0列设置为索引,索引格式为string而不是tuple,而XY坐标的列0和HUC代码的列1。到目前为止,我已经尝试过这些:

  1. 读取csv文件,将索引设置为列中的多索引

    xy_coor = pd.read_csv('xy_huc.csv', index_col=0, header=None)
    xy = pd.DataFrame(xy_coor, index=pd.MultiIndex.from_tuples(xy_coor.index))
    

    但是我有这个错误

    TypeError: Expected tuple, got str
    
  2. index_col=False读取csv,将索引设置为零列

    xy_coor = pd.read_csv('xy_huc.csv', index_col=False, header=None)
    xy = pd.DataFrame(stations_xy, index=pd.MultiIndex.from_tuples(xy_coor[0]))
    

    但是xy索引采用这种形式

xy DataFrame for point 2

我愿意以与保存时相同的格式检索文件。任何建议/建议,表示赞赏。

谢谢

python pandas dictionary gps coordinates
1个回答
0
投票

有多种方法可以做到这一点,但我认为最简单的方法是将x和y保存为单独的列。

另一种方法是解析元组字符串,例如:

def back_to_tuple(input_string):
    # remove punctuation but not comma 
    punctuation = "(/')"
    for punc in punctuation:
        input_string = input_string.replace(punc, '') 

    # split on comma -> list 
    input_string = input_string.split(',')

    # split on comma -> list 
    input_string = input_string.split(',')

    return input_string

您也可以将xy元组另存为以';'分隔的字符串:

def tuple_to_string(input_tuple)
    return ';'.join(input_tuple)

而不是像这样检索它:

def back_to_tuple(input_string):
    return tuple(input_string.split(';'))
© www.soinside.com 2019 - 2024. All rights reserved.