我有一个数据框,其列标题印在下面:
Index(['Unnamed: 0', 'material', 'step', 'zaid', 'mass(gm)', 'activity(Ci)',
'spec.act(Ci/gm)', 'atomden(a/b-cm)', 'atom_frac', 'mass_frac'],
dtype='object')
如果仅尝试获取数据,请说步骤16,然后执行命令:
print (df[(16 in df['step'] == 16)])
一切正常:
Unnamed: 0 material step zaid mass(gm) activity(Ci) spec.act(Ci/gm) atomden(a/b-cm) atom_frac mass_frac
447 447 1 16 90232 2.034000e-09 2.231000e-16 1.097000e-07 9.311000e-12 2.597000e-10 3.048000e-10
448 448 1 16 92233 2.451000e-08 2.362000e-10 9.636000e-03 1.117000e-10 3.116000e-09 3.672000e-09
449 449 1 16 92234 4.525000e-05 2.813000e-07 6.217000e-03 2.053000e-07 5.728000e-06 6.780000e-06
450 450 1 16 92235 1.640000e-01 3.544000e-07 2.161000e-06 7.408000e-04 2.067000e-02 2.457000e-02
451 451 1 16 92236 1.553000e-02 1.004000e-06 6.467000e-05 6.987000e-05 1.949000e-03 2.327000e-03
... ... ... ... ... ... ... ... ... ... ...
37781 37781 10 16 67165 5.941000e-05 0.000000e+00 0.000000e+00 1.195000e-08 3.311000e-07 2.785000e-07
37782 37782 10 16 68166 4.205000e-05 0.000000e+00 0.000000e+00 8.411000e-09 2.330000e-07 1.971000e-07
37783 37783 10 16 68167 1.804000e-05 0.000000e+00 0.000000e+00 3.586000e-09 9.934000e-08 8.457000e-08
37784 37784 10 16 68168 7.046000e-06 0.000000e+00 0.000000e+00 1.393000e-09 3.857000e-08 3.303000e-08
37785 37785 10 16 68170 7.317000e-07 0.000000e+00 0.000000e+00 1.429000e-10 3.958000e-09 3.430000e-09
但是,如果我现在只想获取Zaid 92235的数据(根据上面的步骤16结果显示的数据,该数据显然存在):
print (df[(92235 in df['zaid'] == 92235)])
我收到以下错误:
Traceback (most recent call last):
File "/Users/jack/Library/Python/3.7/lib/python/site-packages/pandas/core/indexes/base.py", line 2890, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: False
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "get_pincell_isos.py", line 57, in <module>
print (df[(92235 in df['zaid'] == 92235)])
File "/Users/jack/Library/Python/3.7/lib/python/site-packages/pandas/core/frame.py", line 2975, in __getitem__
indexer = self.columns.get_loc(key)
File "/Users/jack/Library/Python/3.7/lib/python/site-packages/pandas/core/indexes/base.py", line 2892, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 107, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 131, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1607, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1614, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: False
它显然找不到“ 92235”,即使我知道它存在(如上所示)并且数据存储为int64,与“ step”中的值相同。通过打印“ step”和“ zaid”中的所有值来说明这一点。
print (df['step'])
print (df['zaid'])
给出以下结果:
0 0
1 0
2 0
3 0
4 0
..
37781 16
37782 16
37783 16
37784 16
37785 16
Name: step, Length: 37786, dtype: int64
0 90230
1 90231
2 90232
3 90233
4 90234
...
37781 67165
37782 68166
37783 68167
37784 68168
37785 68170
Name: zaid, Length: 37786, dtype: int64
我希望我缺少明显的东西。我尝试了多种方法来剖析“ zaid”列数据,但没有成功尝试识别与“ zaid”相关的任何值。
谢谢!
答案 0 :(得分:1)
尝试df[df['zaid'] == 92235]
。在任何ipython控制台中尝试以下代码
import pandas as pd
data=data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002, 2003],
'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
df = pd.DataFrame(data)
df['state'] == 'Nevada'
df[df['state'] == 'Nevada']