簡體   English   中英

在熊貓數據框中獲取IndexError

[英]Getting IndexError in pandas dataframe

這段代碼在Python 3.6 pandas 23.x中給了我以下錯誤

IndexError: only integers, slices ( ), numpy.newaxis ( ), ellipsis ( ... ), numpy.newaxis ( None ) 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

該錯誤是鍵盤上的一個松動螺母。

這條線

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

拋出了各種索引和關鍵異常,我們專注於方程的右側。 問題在左邊。 “ ostype”也是數據幀中的字段名稱,當原始名稱“ type”引起問題時,該字段匆匆更改。

角落和門口的人。 看着他們的角落和門口。 或在這種情況下,方程式的兩邊。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM