Python Pandas:代码针对单个值运行,但不针对循环运行。错误:索引必须是单调递增或递减的
当我尝试为值列表运行以下代码时,我得到错误: ->3088 raise VALUE ERROR('索引必须是单调递增或递减') 但是,当我为单个值运行此代码时。它执行 不运行:Python Pandas:代码针对单个值运行,但不针对循环运行。错误:索引必须是单调递增或递减的,python,pandas,analysis,data-science,reindex,Python,Pandas,Analysis,Data Science,Reindex,当我尝试为值列表运行以下代码时,我得到错误: ->3088 raise VALUE ERROR('索引必须是单调递增或递减') 但是,当我为单个值运行此代码时。它执行 不运行: def block(host): time_values = failedIP_df.ix[[host]].set_index(keys='index')['timestamp'] if (return_seconds(time_values[2:3].values[0]) \ - retur
def block(host):
time_values = failedIP_df.ix[[host]].set_index(keys='index')['timestamp']
if (return_seconds(time_values[2:3].values[0]) \
- return_seconds(time_values[0:1].values[0]))<=20:
blocked_host.append(time_values[3:].index.tolist())
list(map(block, failedIP_list))
样本数据:
FailedIP_df:
timestamp index
host
199.72.81.55 01/Jul/1995:00:00:01 0
unicomp6.unicomp.net 01/Jul/1995:00:00:06 1
freenet.edmonton.ab.ca 01/Jul/1995:00:00:12 12
burger.letters.com 01/Jul/1995:00:00:12 14
205.212.115.106 01/Jul/1995:00:00:12 15
129.94.144.152 01/Jul/1995:00:00:13 21
unicomp6.unicomp.net 01/Jul/1995:00:00:07 415
unicomp6.unicomp.net 01/Jul/1995:00:00:08 226
unicomp6.unicomp.net 01/Jul/1995:00:00:21 99
129.94.144.152 01/Jul/1995:00:00:14 41
129.94.144.152 01/Jul/1995:00:00:15 52
129.94.144.152 01/Jul/1995:00:00:17 55
129.94.144.152 01/Jul/1995:00:00:18 75
129.94.144.152 01/Jul/1995:00:00:21 84
示例输出:三次尝试后在20秒内未成功登录的所有主机的索引
blocked_list=[99, 55, 75, 84]
我希望我的代码针对列表中的所有值(即IP地址列表)运行。我真的很感谢你在这方面的帮助。谢谢。打印(df)
print (df)
timestamp index
host
199.72.81.55 01/Jul/1995:00:00:01 0
unicomp6.unicomp.net 01/Jul/1995:00:00:06 1
freenet.edmonton.ab.ca 01/Jul/1995:00:00:12 12
burger.letters.com 01/Jul/1995:00:00:12 14
205.212.115.106 01/Jul/1995:00:00:12 15
129.94.144.152 01/Jul/1995:00:00:13 21
unicomp6.unicomp.net 01/Jul/1995:00:00:07 415
unicomp6.unicomp.net 01/Jul/1995:00:00:08 226
unicomp6.unicomp.net 01/Jul/1995:00:00:33 99 <-change time for matching
129.94.144.152 01/Jul/1995:00:00:14 41
129.94.144.152 01/Jul/1995:00:00:15 52
129.94.144.152 01/Jul/1995:00:00:17 55
129.94.144.152 01/Jul/1995:00:00:18 75
129.94.144.152 01/Jul/1995:00:00:21 84
#convert to datetimes
df.timestamp = pd.to_datetime(df.timestamp, format='%d/%b/%Y:%H:%M:%S')
failedIP_list = ['199.72.81.55', '129.94.144.152', 'unicomp6.unicomp.net']
#filter rows by failedIP_list
df = df[df.index.isin(failedIP_list)]
#get difference and count for all values in index
g = df.groupby(level=0)['timestamp']
DIFF = pd.to_timedelta(g.transform(pd.Series.diff)).dt.total_seconds()
COUNT = g.cumcount()
#filter rows
mask = (DIFF > 20) | (COUNT >= 3)
L = df.loc[mask, 'index'].tolist()
print (L)
[99, 55, 75, 84]
时间戳索引
主办
199.72.81.55 01/Jul/1995:00:00:01 0
unicomp6.unicomp.net 01/Jul/1995:00:00:06 1
freenet.edmonton.ab.ca 01/Jul/1995:00:00:12
burger.letters.com 01/Jul/1995:00:00:12 14
205.212.115.106 01/Jul/1995:00:00:12 15
129.94.144.152 01/Jul/1995:00:00:13 21
unicomp6.unicomp.net 01/Jul/1995:00:00:07 415
unicomp6.unicomp.net 01/Jul/1995:00:00:08 226
unicomp6.unicomp.net 01/Jul/1995:00:00:33 99 20)|(计数>=3)
L=df.loc[掩码,'索引'].tolist()
印刷品(L)
[99, 55, 75, 84]
您能添加样本数据和所需输出吗?@jezrael:我已经添加了样本数据和输出。谢谢。@耶斯雷尔:从昨晚起我就被困在这件事上了。如果你能帮忙,我将不胜感激。我对问题进行了编辑,使其易于理解。如果还有什么,我会尽力解释的。我想我需要一些小的修正,但是我不确定是什么。@DYZ:你能帮我一下吗。
blocked_list=[99, 55, 75, 84]
print (df)
timestamp index
host
199.72.81.55 01/Jul/1995:00:00:01 0
unicomp6.unicomp.net 01/Jul/1995:00:00:06 1
freenet.edmonton.ab.ca 01/Jul/1995:00:00:12 12
burger.letters.com 01/Jul/1995:00:00:12 14
205.212.115.106 01/Jul/1995:00:00:12 15
129.94.144.152 01/Jul/1995:00:00:13 21
unicomp6.unicomp.net 01/Jul/1995:00:00:07 415
unicomp6.unicomp.net 01/Jul/1995:00:00:08 226
unicomp6.unicomp.net 01/Jul/1995:00:00:33 99 <-change time for matching
129.94.144.152 01/Jul/1995:00:00:14 41
129.94.144.152 01/Jul/1995:00:00:15 52
129.94.144.152 01/Jul/1995:00:00:17 55
129.94.144.152 01/Jul/1995:00:00:18 75
129.94.144.152 01/Jul/1995:00:00:21 84
#convert to datetimes
df.timestamp = pd.to_datetime(df.timestamp, format='%d/%b/%Y:%H:%M:%S')
failedIP_list = ['199.72.81.55', '129.94.144.152', 'unicomp6.unicomp.net']
#filter rows by failedIP_list
df = df[df.index.isin(failedIP_list)]
#get difference and count for all values in index
g = df.groupby(level=0)['timestamp']
DIFF = pd.to_timedelta(g.transform(pd.Series.diff)).dt.total_seconds()
COUNT = g.cumcount()
#filter rows
mask = (DIFF > 20) | (COUNT >= 3)
L = df.loc[mask, 'index'].tolist()
print (L)
[99, 55, 75, 84]