从CSV文件中操纵两个Col



首先,让我告诉我我花了将近1小时的时间来测试许多可能的相关问题,但没有成功。这对我来说很复杂(初学者用户(。如果我按数字键入编号,我会更早完成。但是这个想法是总是学习。所以我想了解。

考虑以下示例:

>>> np.random.rand(3,2)
array([[ 0.14022471,  0.96360618],
       [ 0.37601032,  0.25528411],
       [ 0.49313049,  0.94909878]])

这是我需要在某些Voronoi代码中使用的对象。但是我的数据来自CSV文件。

我有一个带标头的CSV文件,我需要列clatclong。我想获得这样的输出:

array([[ clat_1,  clong_1],
       [ clat_2,  clong_2],
                ...
       [ clat_N,  clong_N]])

因此,使用print,输出将是这样的:

[[ 0.19151945  0.62210877]
 [ 0.43772774  0.78535858]
 [ 0.77997581  0.27259261]
 [ 0.39720258  0.78873014]
 [ 0.31683612  0.56809865]]

我用

加载了CSV文件
csv_file='./demog.csv'
demog = np.genfromtxt(csv_file, delimiter=',', skip_header=0, skip_footer=0, names=True,dtype=None)

我尝试了clat=demog['clat']clong=demog['clong'],但print clat似乎不是一个列。

如何创建这样的N x 2对象(我什至不知道其名称:数组,列表,矩阵,表,...(

clat将为您提供整个LAT列,Clong将为您提供整个长列。要像您提到的[LAT,长]对一样,将它们放在一个数组中,您可以使用zip:

array = []
for lat, long in zip(clat, clong):
    array.append([lat, long])
array = np.array(array)
print array

最新更新