Nested If, working like an excel SUMIF for two unequal lists summing the distance if GPS timestamp meets criteria This is my first post, I am very new to coding and Python especially,

This code intends to do an excel SUMIF between two tables with different indexes. The first tables has GPS data with timestamp, vehicle ID and distance The second table has vehicle ID and timestamps of events I want to measure the distance run during events

Thanks

for x in range(1,34):
df = pd.read_csv("file"
                 + str(x) + '.csv',
                 parse_dates=[10])

red = 0
green = 0
black = 0

output = [[], [], [], []]
for i in range(len(lista[1])):
    for j in range(len(listc[1])):
        if listc[1][j] &lt;= lista[3][i] or listc[1][j] &gt;= lista[2][i]:
            if lista[7][i] &gt;= listc[1][j] and lista[6][i] &lt;= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] == 'intended value' :
                red += listc[2][i]
            if lista[3][i] &gt;= listc[1][j] and lista[7][i] &lt;= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] != 'intended value' :
                red += listc[2][i]
            if lista[6][i] &gt;= listc[1][j] and lista[2][i] &lt;= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] == 'intended value' :
                green += listc[2][i]
            if lista[7][i] &gt;= listc[1][j] and lista[2][i] &lt;= listc[1][j] and lista[0][i] == listc[0][j] and lista[8][i] != 'intended value' :
                green += listc[2][i]
            if lista[2][i] &gt;= listc[1][j] and lista[3][i - 1] &lt;= listc[1][j] and lista[0][i] == listc[0][j]:
                black += listc[2][i]
    toc = timeit.default_timer()
    if i % 100 == 0:
        print('processing algorithm: {}'.format(toc - tic))
        print('we are at row {}'.format(i))
    output[0].append(lista[1][i])
    output[1].append(red)
    output[2].append(green)
    output[3].append(black)
    red = 0
    green = 0
    black = 0
toc = timeit.default_timer()
np.savetxt("outfile" + str(x)
           + ".csv", np.column_stack((output[0], output[1], output[2], output[3])), delimiter=",", fmt='%s')
tac = timeit.default_timer()
print('exporting {}'.format(tac - toc))
For me, the problem starts with the nested loops showing no specification of what is to be achieved, not even a suggested abstraction (being the body of a function given a name).  

Observations:


the output does seem to depend on the order of elements of lista
(even beyond its order: lista[3][i-1])
• hope lista[2][i] &gt;= listc[1][j] is never True for i 0
 (unless you want lista[3][-1] accessed)
the output does not seem to depend on the order of elements of listc  
both lista and listc are not changed
→ the "range conditions" won't change unless at least one index changes  
all of "the increments" share the condition lista[0][i] == listc[0][j]
the conditions between lista[6/7][i] and listc[1][j] are not complementary for including equality in both cases


implying red/green possibly getting incremented twice in a single iteration (not using else)



idea:  


document, in the code, what is to be achieved
Python supports this with docstrings
use telling names
have a tool help you sticking to The Python Style Guide
order listc
for each i, iterate only that part of the ordered listc where lista[0][i] == listc[0][j]
ignore if lista and listc are not "rectangular":


food for thought: untested result of refactoring (get tool support for such, too)
(here extracting local variables, mostly)

list_c = sorted(listc)
for i in range(len(lista[1])):
    red = green = black = 0
    a0i = lista[0][i]
    first = bisect_left(list_c[1], a0i)
    beyond = bisect_right(list_c[1], a0i, first)
    if first &lt; beyond:
        a2i = lista[2][i]
        a3i = lista[3][i]
        c2i = list_c[2][i]
        a8i_intended = lista[8][i] == 'intended value'
        for j in range(first, beyond):
            c1j = list_c[1][j]
            if (c1j &lt;= a3i or c1j &gt;= a2i):
                if lista[7][i] &gt;= c1j and lista[6][i] &lt;= c1j and a8i_intended:
                    red += c2i
                if a3i &gt;= c1j and lista[7][i] &lt;= c1j and not a8i_intended:
                    red += c2i
                if lista[6][i] &gt;= c1j and a2i &lt;= c1j and a8i_intended:
                    green += c2i
                if lista[7][i] &gt;= c1j and a2i &lt;= c1j and not a8i_intended:
                    green += c2i
                if a2i &gt;= c1j and lista[3][i - 1] &lt;= c1j:
                    black += c2i
    toc = timeit.default_timer()
    if i % 100 == 0:
        print('processing algorithm: {}'.format(toc - tic))
        print('we are at row {}'.format(i))
    output[0].append(lista[1][i])
    output[1].append(red)
    output[2].append(green)
    output[3].append(black)


afterthought:  it may be better to handle listc[1][j] &lt;= lista[3][i] and lista[2][i] &lt;= listc[1][j] separately