while-loop:列表索引超出范围过滤数据帧
while-loop: list index out of range filtering dataframe
基本上我有一个充满数字标识符的列表——我使用这些数字标识符作为过滤数据帧的条件,然后一旦 df 被过滤,我试图存储过滤后的数据帧的长度作为新的单独数据框中的值。
我使用我的数字标识符列表中的最后一个值(例如 list[-1]
)作为我循环的停止点——我这样做是为了让循环 运行 通过所有标识符并在完成最后一个标识符后完成 - 我认为这可能是问题所在。
我的代码在遍历列表中的所有唯一数字标识符时吐出了所有正确的长度——但是,它仍然给我一个索引超出范围的错误(如下所示)。
def get_frames(U_id):
k = sorted(df.trackId.unique())
#k is the sorted list of unique numerical identifiers
i = 0
maximum = k[-1] #am using the final value in the list as the stopping point for the loop
while i <= maximum:
condition = df.trackId == k[i]
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
i+=1
return df2
get_frames('1CCM0701')
36
18
37
4
33
25
27
49
46
12
45
24
4
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-120-3252dfb603ae> in <module>
13 return df2
14
---> 15 get_frames('1CCM0701')
<ipython-input-120-3252dfb603ae> in get_frames(U_id)
6 maximum = k[-1]
7 while i <= maximum:
----> 8 condition = df.trackId == k[i]
9 df2 = df[condition]
10 values = print(len(df2))
IndexError: list index out of range
这里的问题是,当您使用通过索引访问数组的迭代变量时,您使用 k[-1] 作为停止点。 k[-1]是36,这显然与你数组的长度无关。相反,您应该使用 for 循环,或者将 i 与数组的长度进行比较。
Python For 循环:
for i, val in enumerate(k):
condition = df.trackId == val
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
传统的 For 循环
for i in range(len(k))
condition = df.trackId == k[i]
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
While 循环
arrLen = len(k)
while i < arrLen:
condition = df.trackId == k[i]
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
i+=1
基本上我有一个充满数字标识符的列表——我使用这些数字标识符作为过滤数据帧的条件,然后一旦 df 被过滤,我试图存储过滤后的数据帧的长度作为新的单独数据框中的值。
我使用我的数字标识符列表中的最后一个值(例如 list[-1]
)作为我循环的停止点——我这样做是为了让循环 运行 通过所有标识符并在完成最后一个标识符后完成 - 我认为这可能是问题所在。
我的代码在遍历列表中的所有唯一数字标识符时吐出了所有正确的长度——但是,它仍然给我一个索引超出范围的错误(如下所示)。
def get_frames(U_id):
k = sorted(df.trackId.unique())
#k is the sorted list of unique numerical identifiers
i = 0
maximum = k[-1] #am using the final value in the list as the stopping point for the loop
while i <= maximum:
condition = df.trackId == k[i]
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
i+=1
return df2
get_frames('1CCM0701')
36
18
37
4
33
25
27
49
46
12
45
24
4
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-120-3252dfb603ae> in <module>
13 return df2
14
---> 15 get_frames('1CCM0701')
<ipython-input-120-3252dfb603ae> in get_frames(U_id)
6 maximum = k[-1]
7 while i <= maximum:
----> 8 condition = df.trackId == k[i]
9 df2 = df[condition]
10 values = print(len(df2))
IndexError: list index out of range
这里的问题是,当您使用通过索引访问数组的迭代变量时,您使用 k[-1] 作为停止点。 k[-1]是36,这显然与你数组的长度无关。相反,您应该使用 for 循环,或者将 i 与数组的长度进行比较。
Python For 循环:
for i, val in enumerate(k):
condition = df.trackId == val
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
传统的 For 循环
for i in range(len(k))
condition = df.trackId == k[i]
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
While 循环
arrLen = len(k)
while i < arrLen:
condition = df.trackId == k[i]
df2 = df[condition]
values = print(len(df2))
df2 = pd.DataFrame({U_id:values}, index = [i])
i+=1