我使用Pandas导入了Excel表格。该表包含四列,表示节点,X,Y和Z数据。我使用了以下脚本:
import pandas as pd
SolidFixity = pd.read_excel('GeomData.xlsx', sheetname = 'SurfaceFixitySolid')
接下来我要做的是使用过滤器表过滤此数据帧以选择感兴趣的节点。我用了这个命令:
SolidFixity.filter(like = '797', axis = 'Nodes')
这不起作用并抛出以下错误:
ValueError:对象类型
没有名为Nodes的轴
我知道有一个名为Nodes的轴,因为以下命令:
In[17]SolidFixity.axes
输出以下内容:
Out[17]:
[RangeIndex(start=0, stop=809, step=1),
Index(['Nodes', 'X', 'Y ', 'Z'], dtype='object')]
节点就在那里,像太阳一样闪闪发光。
我在这里做错了什么?
答案 0 :(得分:2)
您需要boolean indexing
或query
使用contains
掩码,或与797
进行完全匹配:
SolidFixity = pd.DataFrame({'Nodes':['797','sds','797 dsd','800','s','79785'],
'X':[5,3,6,9,2,4]})
print (SolidFixity)
Nodes X
0 797 5
1 sds 3
2 797 dsd 6
3 800 9
4 s 2
5 79785 4
a = SolidFixity[SolidFixity.Nodes.str.contains('797')]
print (a)
Nodes X
0 797 5
2 797 dsd 6
5 79785 4
b = SolidFixity[SolidFixity.Nodes == '797']
print (b)
Nodes X
0 797 5
b = SolidFixity.query("Nodes =='797'")
print (b)
Nodes X
0 797 5
filter
函数可能只有轴值:
axis:int或string axis name
要过滤的轴。默认情况下,这是信息轴,系列为
index
,DataFrame为columns
并按参数like
,regex
和items
返回所有列:
df = pd.DataFrame({'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C797':[7,8,9,4,2,3],
'797':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'F':list('aaabbb')})
print (df)
797 A B C797 E F
0 1 a 4 7 5 a
1 3 b 5 8 3 a
2 5 c 4 9 6 a
3 7 d 5 4 9 b
4 1 e 5 2 2 b
5 0 f 4 3 4 b
a = df.filter(like = '797', axis = 1)
#same as
#a = df.filter(like = '797', axis = 'columns')
print (a)
797 C797
0 1 7
1 3 8
2 5 9
3 7 4
4 1 2
5 0 3
c = df.filter(items = ['797'], axis = 1)
#same as
#c = df.filter(items = ['797'], axis = 'columns')
print (c)
797
0 1
1 3
2 5
3 7
4 1
5 0