【问题标题】:Indexer is out-of-bounds error. Using df loc and iloc to get the column value that meets a specific criteria索引器越界错误。使用 df loc 和 iloc 获取满足特定条件的列值
【发布时间】:2023-03-21 14:48:01
【问题描述】:

我有一个如下所示的数据框。

>>> df
         Status    server Name   Location                    DateTime
0     [RUNNING]        server1     London  2021-02-08 16:55:43.770558
1     [RUNNING]      server07a     London  2021-02-08 16:55:43.770558
2     [RUNNING]      server07b     London  2021-02-08 16:55:43.770558
3     [RUNNING]    labserver01     London  2021-02-08 16:55:43.770558
4  [NOTRUNNING]      server04a     London  2021-02-08 16:55:43.770558
5  [NOTRUNNING]      server04b     London  2021-02-08 16:55:43.770558
6     [RUNNING]       server05     London  2021-02-08 16:55:43.770558
7     [RUNNING]       server06     London  2021-02-08 16:55:43.770558
8     [RUNNING]       server08     London  2021-02-08 16:55:43.770558

我正在尝试获取状态为 NOTRUNNING 的服务器的名称。如果所有内容都显示为 RUNNING,则验证变量应该填充每个条目的“无”列表。如果有一个项目显示 NOTRUNNING,那么它应该写 serverName 而不是 NONE。

def errorstatuschecker(df):
   affectedserver = []
   #print(df)
   for i in df['Status'].values:
      if i != '[RUNNING]':
         for j in range(len(df.index)):
            print(df.loc[df['Status'] == '[RUNNING]', 'server Name'].iloc[j])
      else:
         affectedserver.append('None')
   verify = len(affectedserver) > 0 and all(elem == affectedserver[0] for elem in affectedserver)

我收到以下错误。我明白为什么。但我无法理解如何更好地编写这个,所以我不会用完索引。

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "C:\Users\Python39\lib\site-packages\pandas\core\indexing.py", line 895, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "C:\Users\Python39\lib\site-packages\pandas\core\indexing.py", line 1501, in _getitem_axis
    self._validate_integer(key, axis)
  File "C:\Users\Python39\lib\site-packages\pandas\core\indexing.py", line 1444, in _validate_integer
    raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

【问题讨论】:

    标签: python pandas indexoutofboundsexception


    【解决方案1】:

    你为什么不试试这个:

    idx = df.index[df['Status']=="[NOTRUNNING]"].tolist()
    df.loc[idx]
    

    我认为它会运行得更快。如果您只想将服务器名称保存在新的数据框中:

    df2 = df['server Name'].loc[idx]
    

    【讨论】:

    • 谢谢!这也有帮助。不完全适合我现在使用的场景。但这也适用于非常相似的操作。
    【解决方案2】:

    找到了一种导航方式,仍然可以得到我想要的东西。可能不是很聪明。但现在有效。任何更好的方法仍然是受欢迎的。

    def errorstatuschecker(df):
       affectedserver = list(df.loc[df['Status'] != '[RUNNING]', 'server Name'].to_numpy())
       if len(affectedserver) == 0:
          msg = errormessages.msg6
          notify(msg)
       else:
          affectedsystems = '\n'.join(map(str, affectedserver))
          msg = errormessages.msg1
          subject = errormessages.sub1
          notify(msg,affectedsystems)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 2015-03-22
      相关资源
      最近更新 更多