【发布时间】: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