python - Error: list indices must be integers, not Series -
i have grabbed column pandas data frame , made list rows. if print first 2 values of list, output:
print dflist[0] print dflist[1] 0 0.00 1 0.00 2 0.00 3 0.00 4 0.00 5 0.11 6 0.84 7 1.00 8 0.27 9 0.00 10 0.52 11 0.55 12 0.92 13 0.00 14 0.00 ... 50 0.42 51 0.00 52 0.00 53 0.00 54 0.40 55 0.65 56 0.81 57 1.00 58 0.54 59 0.21 60 0.00 61 0.33 62 1.00 63 0.75 64 1.00 name: 9, length: 65, dtype: float64 65 1.00 66 1.00 67 1.00 68 1.00 69 1.00 70 1.00 71 0.55 72 0.00 73 0.39 74 0.51 75 0.70 76 0.83 77 0.87 78 0.85 79 0.53 ... 126 0.83 127 0.83 128 0.83 129 0.71 130 0.26 131 0.11 132 0.00 133 0.00 134 0.50 135 1.00 136 1.00 137 0.59 138 0.59 139 0.59 140 1.00 name: 9, length: 76, dtype: float64
when try iterate on list loop, error message:
for in dflist: print dflist[i] traceback (most recent call last): file "./windows.py", line 84, in <module> print dflist[i] typeerror: list indices must integers, not series
if write code as:
for in dflist: print
i correct output:
0 0.00 1 0.00 2 0.00 3 0.00 4 0.00 5 0.11 6 0.84 7 1.00 8 0.27 9 0.00 10 0.52 11 0.55 12 0.92 13 0.00 14 0.00 ... 50 0.42 51 0.00 52 0.00 53 0.00 54 0.40 55 0.65 56 0.81 57 1.00 58 0.54 59 0.21 60 0.00 61 0.33 62 1.00 63 0.75 64 1.00 name: 9, length: 65, dtype: float64 ... ... name: 9, length: 108, dtype: float64 507919 0.00 507920 0.83 507921 1.00 507922 1.00 507923 0.46 507924 0.83 507925 1.00 507926 1.00 507927 1.00 507928 1.00 507929 1.00 507930 1.00 507931 1.00 507932 1.00 507933 1.00 ... 508216 1 508217 1 508218 1 508219 1 508220 1 508221 1 508222 1 508223 1 508224 1 508225 1 508226 1 508227 1 508228 1 508229 1 508230 1 name: 9, length: 312, dtype: float64
i not know why happens.
i want iterate through list 5 consecutive "windows" , calculate means , put these means in list.
in python when write for in
i
element not index need print i
not print dflist[i]
.
either of following 2 options fine:
for in dflist[i]: print in range(len(dflist)): print dflist[i]
the first more pythonic , elegant unless need index.
edit
as jwilner suggested can do:
for i, element in enumerate(dflist): print i, element
here i
index , element
dflist[i]
.