简体   繁体   English

使用xlrd和python遇到列表和矩阵问题

[英]Having trouble with lists and matrix using xlrd and python

So my problem is to take a couple of long vectors (each being 15000 rows long) with the appearence: 所以我的问题是采用几个长矢量(每个长15000行)并出现:

Origin     Destination     Distance

and the corresponding values in the columns. 以及列中的相应值。 I want however to convert these, using Python and the xlrd package, into a distance matrix having 但是我想使用Python和xlrd包将这些转换为具有

          Destination1     Destination2
Origin1   Distance11       Distance12
Origin2   Distance21       Distance22

and so forth. 等等。

What I have tried thus far is: 到目前为止,我尝试过的是:

matrix ={}, i=0, list3 = [], list1 = []
for row in range(orksheet.nrows):
matrix[i] = {}
cell = worksheet.cell(row,2)
distance = cell.value
if float(distance) < 25000:
    list1 = [int(worksheet.cell_value(row,0))]
    list3 = list3.append(list1)
    list2 = [int(worksheet.cell_value(row,1))]
    for l in list1:
        for j in list2:
                matrix[l, j]=math.ceil(worksheet.cell_value(row,2))             
i+=1

This works somewhat. 这有点奏效。 When I use print(l,j,matrix[l,j] 当我使用print(l,j,matrix [l,j]

within the loop over l and j I get what I get the desired values. 在l和j的循环中,我得到了所需的值。 However, using print(matrix) gives the (general, ie the output like that but with the corresponding values instead) output: 但是,使用print(matrix)会给出(常规,即类似的输出,但具有相应的值)输出:

(Origin, Destination): Distance and sometimes: distance: {}, distance: {}, 

and so on. 等等。

What I've perceived is the problem is with the matrix. 我已经意识到问题出在矩阵上。 I cannot understand why it prints like that which I believe has something to do with the lists? 我不明白为什么打印出来的照片与我认为与列表有关? The list1 and list2 has len 1 which seems odd to me. list1和list2有len 1,这对我来说似乎很奇怪。 I've tried to use list3 to append list1 but it also get len 1. 我尝试使用list3追加list1,但它也得到len 1。

Regards, 问候,

I could not recommend pandas more for data-manipulation tasks. 对于数据处理任务,我不能推荐更多熊猫

For example, the operation you seek in pandas is called pivot : 例如,您在大熊猫中寻求的操作称为ivot

In [11]: df = pd.DataFrame({'origin': list('aabbccdd'), 'destination': ['d1', 'd2'] * 4, 'distance': np.arange(8)})

In [12]: df
Out[12]: 
  destination  distance origin
0          d1         0      a
1          d2         1      a
2          d1         2      b
3          d2         3      b
4          d1         4      c
5          d2         5      c
6          d1         6      d
7          d2         7      d

In [13]: df.pivot('origin', 'destination', 'distance')
Out[13]: 
destination  d1  d2
origin             
a             0   1
b             2   3
c             4   5
d             6   7

And to read actual excel file there's pandas.read_excel which AFAIR uses xlrd under the hood: 要读取实际的excel文件,有pandas.read_excel ,AFAIR在后台使用xlrd:

df = read_excel('path_to_file.xls', 'Sheet1', index_col=None, na_values=['NA'])

And there's a lot more to find in the documentation 文档中还有更多内容可以找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM