gpt4 book ai didi

python - 使用 Pandas 过滤 Excel 表格

转载 作者:行者123 更新时间:2023-12-01 02:34:52 30 4
gpt4 key购买 nike

我已经使用 Pandas 导入了 Excel 表格。该表包含四列,分别代表节点、X、Y 和 Z 数据。我使用了以下脚本:

import pandas as pd
SolidFixity = pd.read_excel('GeomData.xlsx', sheetname = 'SurfaceFixitySolid')

接下来我想做的是使用过滤表过滤此数据帧以选择感兴趣的节点。我使用了这个命令:

SolidFixity.filter(like = '797', axis = 'Nodes')

这不起作用并引发以下错误:

ValueError: No axis named Nodes for object type

我知道有一个名为 Nodes 的轴,因为以下命令:

In[17]SolidFixity.axes

输出以下内容:

Out[17]: 
[RangeIndex(start=0, stop=809, step=1),
Index(['Nodes', 'X', 'Y ', 'Z'], dtype='object')]

节点就在那里,像太阳一样闪闪发光。

我在这里做错了什么?

最佳答案

看来你需要boolean indexingquery带面具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 or string axis name

The axis to filter on. By default this is the info axis, index for Series, columns for DataFrame

并通过参数返回所有列regexitems:

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

关于python - 使用 Pandas 过滤 Excel 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46365838/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com