首页 > 解决方案 > 我无法切片和修改我的数组以从中提取所需的信息

问题描述

我想通过 pandas.read_excel 读取一个 excel 文件,但第一行只是列中数据的指示,我不希望它被导入。我使用此代码跳过第一行:

nodes=pd.read_excel(filename,skiprows=1)

但它操纵了第二行,这是我感兴趣的信息。这是我的第一个问题...

import numpy as np
import matplotlib.pyplot as plt
import time
import pandas as pd 
import openpyxl as pxl
# import xlrd
print('hi')
FilePatch='E:\\# Civil Engineering Undergraduate\\Projects\\Python\\Frame'
NodesFile=FilePatch+'\\nodes.xlsx'
MemsFile=FilePatch+'\\members.xlsx'
MatsFile=FilePatch+'\\sections.xlsx' 
#print(NodesFile)
#print(MemsFile)
#print(MatsFile) 
nodes=pd.read_excel(NodesFile)
nodes=pd.DataFrame(nodes)
mems=pd.read_excel(MemsFile,skiprows=1)
mats=pd.read_excel(MatsFile,skiprows=1)

print(nodes)
print(mems)
print(mats)
Segments=100
Scale=1 
print(nodes[:,0])

而且当我尝试通过这种方式切片从这个excel文件中提取所需的信息时:

n=np.size(nodes[:,0])

因为我对文件中的行数感兴趣,以便以正确的尺寸启动我的数组,但不幸的是它通过了一个异常。

Exception has occurred: TypeError
'(slice(None, None, None), 0)' is an invalid key
File "E:\# Civil Engineering Undergraduate\Projects\Python\Frame\Frame.py", 
line 26, in <module>
n=np.size(nodes[:,0])

我想要这个数据:

0       1   0  0.0   1   1   1   0   0   0   0   0   0   0   0   0
1       2   0  3.0   0   0   0   0   0   0   0   0   0   0   0   0
2       3   0  6.0   0   0   0   0   0   0   0   0   0   0   0   0
3       4   0  9.0   0   0   0   0   0   0   0   0   0   0   0   0
4       5   5  0.0   0   1   1   0   0   0   0   0   0   0   0   0
5       6   5  3.0   0   0   0   0   0   0   0   0   0   0   0   0
6       7   5  6.0   0   0   0   0   0   0   0   0   0   0   0   0
7       8   5  9.0   0   0   0   0   0   0   0   0   0   0   0   0
8       9  13  0.0   1   1   0   0   0   0   0   0   0   0   0   0
9      10  13  3.0   0   0   0   0   0   0   0   0   0   0   0   0
10     11  13  6.0   0   0   0   0   0   0   0   0   0   0   0   0
11     12  13  9.0   0   0   0   0   0   0   0   0   0   0   0   0
12     13  17  0.0   1   1   1   0   0   0   0   0   0   0   0   0
13     14  17  3.0   0   0   0   0   0   0   0   0   0   0   0   0
14     15  17  4.5   0   0   0   0   0   0   0   0   0   0   0   0
15     16  17  6.0   0   0   0   0   0   0   0   0   0   0   0   0
16     17  17  9.0   0   0   0   0   0   0   0   0   0   0   0   0
17     18  22  3.0   1   1   1   0   0   0   0   0   0   0   0   0
18     19  22  4.5   0   0   0   0   0   0   0   0   0   0   0   0
19     20  24  4.5   0   0   0   0   0   0   0   0   0   0   0   0

但是如果我使用skirows,它会给我这个!!(看第一行):

     1   0  0.1  1.1  1.2  1.3  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  0.10
0    2   0  3.0    0    0    0    0    0    0    0    0    0    0    0     0
1    3   0  6.0    0    0    0    0    0    0    0    0    0    0    0     0
2    4   0  9.0    0    0    0    0    0    0    0    0    0    0    0     0
3    5   5  0.0    0    1    1    0    0    0    0    0    0    0    0     0
4    6   5  3.0    0    0    0    0    0    0    0    0    0    0    0     0
5    7   5  6.0    0    0    0    0    0    0    0    0    0    0    0     0
6    8   5  9.0    0    0    0    0    0    0    0    0    0    0    0     0
7    9  13  0.0    1    1    0    0    0    0    0    0    0    0    0     0
8   10  13  3.0    0    0    0    0    0    0    0    0    0    0    0     0
9   11  13  6.0    0    0    0    0    0    0    0    0    0    0    0     0
10  12  13  9.0    0    0    0    0    0    0    0    0    0    0    0     0
11  13  17  0.0    1    1    1    0    0    0    0    0    0    0    0     0
12  14  17  3.0    0    0    0    0    0    0    0    0    0    0    0     0
13  15  17  4.5    0    0    0    0    0    0    0    0    0    0    0     0
14  16  17  6.0    0    0    0    0    0    0    0    0    0    0    0     0
15  17  17  9.0    0    0    0    0    0    0    0    0    0    0    0     0
16  18  22  3.0    1    1    1    0    0    0    0    0    0    0    0     0
17  19  22  4.5    0    0    0    0    0    0    0    0    0    0    0     0
18  20  24  4.5    0    0    0    0    0    0    0    0    0    0    0     0

标签: pythonarrayspandastypesslice

解决方案


我解决了这个!

import numpy as np
import matplotlib.pyplot as plt
import time
import pandas as pd
FilePatch='E:\\# Civil Engineering Undergraduate\\Projects\\Python\\Frame'
NodesFile=FilePatch+'\\nodes4.xlsx'
MemsFile=FilePatch+'\\members4.xlsx'
MatsFile=FilePatch+'\\sections4.xlsx' 
nodes=pd.read_excel(NodesFile)
mems=pd.read_excel(MemsFile)
mats=pd.read_excel(MatsFile)

nodes=np.array(nodes)
mems=np.array(mems)
mats=np.array(mats)

推荐阅读