索引器越界错误。使用 df loc 和 iloc 获取满足特定条件的列值
Indexer is out-of-bounds error. Using df loc and iloc to get the column value that meets a specific criteria
我有一个如下所示的数据框。
>>> 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
正在尝试获取状态为 NOT运行 的服务器的名称。如果所有内容都显示 运行,验证变量应该用每个条目 'None' 的列表填充。如果有一项显示 NOT运行,则应该写 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
找到了一种四处导航并仍然得到我想要的东西的方法。可能不是很聪明。但现在有效。任何更好的方法仍然欢迎。
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)
你为什么不试试这个:
idx = df.index[df['Status']=="[NOTRUNNING]"].tolist()
df.loc[idx]
我认为 运行 会更快。如果您只想将服务器名称保存在新数据框中:
df2 = df['server Name'].loc[idx]
我有一个如下所示的数据框。
>>> 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
正在尝试获取状态为 NOT运行 的服务器的名称。如果所有内容都显示 运行,验证变量应该用每个条目 'None' 的列表填充。如果有一项显示 NOT运行,则应该写 serverName.
而不是 NONEdef 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
找到了一种四处导航并仍然得到我想要的东西的方法。可能不是很聪明。但现在有效。任何更好的方法仍然欢迎。
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)
你为什么不试试这个:
idx = df.index[df['Status']=="[NOTRUNNING]"].tolist()
df.loc[idx]
我认为 运行 会更快。如果您只想将服务器名称保存在新数据框中:
df2 = df['server Name'].loc[idx]