Python 在数据帧中获取索引器
这段代码给出了Python 3.6.23.x中的以下错误Python 在数据帧中获取索引器,python,pandas,Python,Pandas,这段代码给出了Python 3.6.23.x中的以下错误 索引器错误:只有整数、片(:)、省略号(…)、numpy.newaxis(None)和整数或布尔数组是有效的索引 在线 ostype[asset]=dfx.ostype(0)#获取与[0]相同的错误 我不清楚为什么。这是在Jupyter中运行的完整代码 from collections import defaultdict assets = defaultdict(list) warnings = defaultdict(list) re
索引器错误:只有整数、片(
:)、省略号(
…)、numpy.newaxis(
None)和整数或布尔数组是有效的索引
在线
ostype[asset]=dfx.ostype(0)
#获取与[0]相同的错误
我不清楚为什么。这是在Jupyter中运行的完整代码
from collections import defaultdict
assets = defaultdict(list)
warnings = defaultdict(list)
result = defaultdict(list)
for asset in servers:
result[asset] = 'Fail'
# This filters to only the records for this asset
dfx = df[df['server'] == asset]
ostype[asset] = dfx.ostype(0) ### Error
for ntp in dfx.source:
if ntp in valid_ntp_servers:
result[asset] = 'Pass'
assets[asset].append(ntp)
else:
warnings[asset].append(ntp)
我把代码放在一个单独的Jupyter单元格中,得到了完全符合预期的结果
test = dfx.ostype[0]
print(test)
linux
附加测试
for asset in servers[:5]:
dfx = df[df['server'] == asset]
test = dfx.ostype[0]
print(dfx)
print(test)
导致以下异常
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-388-010067e6d390> in <module>()
2 for asset in servers[:5]:
3 dfx = df[df['server'] == asset]
----> 4 test = dfx.ostype[0]
5 print(dfx)
6 print(test)
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
765 key = com._apply_if_callable(key, self)
766 try:
--> 767 result = self.index.get_value(self, key)
768
769 if not is_scalar(result):
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
3116 try:
3117 return self._engine.get_value(s, k,
-> 3118 tz=getattr(series.dtype, 'tz', None))
3119 except KeyError as e1:
3120 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0
按照注释中的建议使用.iloc[0]在我的测试循环中起作用,但在我的程序中使用它时失败
IndexError Traceback (most recent call last)
<ipython-input-390-fce3c88d6e8e> in <module>()
6 result[asset] = 'Fail'
7 dfx = df[df['server'] == asset]
----> 8 ostype[asset] = dfx.ostype.iloc[0]
9 for ntp in dfx.source:
10 if ntp in valid_ntp_servers:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
索引器错误回溯(最近一次调用)
在()
6结果[资产]=“失败”
7 dfx=df[df['server']==资产]
---->8 ostype[asset]=dfx.ostype.iloc[0]
9对于dfx中的ntp。来源:
10如果ntp在有效的ntp服务器中:
索引器错误:只有整数、片(`:`)、省略号(`…`)、numpy.newaxis(`None`)和整数或布尔数组是有效的索引
这个错误是键盘上的一个松动的螺母
这条线
ostype[asset] = dfx.ostype.iloc[0]
抛出了各种索引和关键异常,我们将重点放在等式的右侧。问题在左边。“ostype”也是数据帧中的一个字段名,当原始名称“type”导致问题时,该字段名被匆忙更改
街角和门口。观察他们的角落和门口。或者在这种情况下,方程的两边
谢谢试着用
[0]
而不是(0)
@Shaido我很确定你刚刚抓到了:)@Shaido我也试过了,但有同样的错误。我尝试了每一种变体(.loc、astype等),我知道它应该在没有成功的情况下工作,但无法解释原因。它一直工作,直到我把它放入一个循环,然后我开始得到keyerror或Indexer
ostype[asset] = dfx.ostype.iloc[0]