在熊猫数据框中获取IndexError

时间:2018-08-28 03:31:59

标签: python pandas

这段代码在Python 3.6 pandas 23.x中给我以下错误

IndexError: only integers, slices (), ellipsis ( ... ), numpy.newaxis () and integer or boolean arrays are valid indices

在线

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

循环的输出如下:

  ostype        date   server            source
0  linux  2018-08-14  abcde01  east.time.zzzzzz
1  linux  2018-08-14  abcde01       ntp7.zzzzzz
2  linux  2018-08-14  abcde01       ntp9.zzzzzz
3  linux  2018-08-14  abcde01       ntp0.zzzzzz
linux

使用注释中建议的.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

1 个答案:

答案 0 :(得分:1)

该错误是键盘上的一个松动螺母。

此行

ostype[asset] = dfx.ostype.iloc[0]

抛出了各种索引和关键异常,我们集中在等式的右侧。问题在左边。 “ ostype”也是数据帧中的字段名称,当原始名称“ type”引起问题时,该字段匆匆更改。

角落和门口的人。看着他们的角落和门口。或在这种情况下,等式的两边。

谢谢