lundi 12 juillet 2021

"if" clause syntax using addition and multiplication

I have a very nasty DataFrame where every 5 rows are the same. (Hence why the "if" clauses range from 0-5). I'm trying to write repeatable logic against these 5 rows, but still iterate over hundreds of rows.

In the 1st, 2nd and 10th IF clauses, I have the number zero added to r times 1. (this goes 0,8,15, etc)

However, in the 3rd, 5th, 6th, 7th, 8th & 9th IF clauses, I have a non-zero number being added to r*1. (this goes 1,9,16, etc)

What is wrong with the non-zero versions? Notice the length of the resulting arrays - they are length zero - whereas the other arrays are length 5.

for r in list(range(0,rows,7)):
    for c in list(range(0,columns)):
        if r==0+r*1 and c==0:
            first_name.append(df.iloc[r][c])
        if r==0+r*1 and c==2:
            date.append(df.iloc[r][c])
        if r==1+r*1 and c==0:
            last_name.append(df.iloc[r][c])
        if r==0+r*1 and c==5:
            cash_amount.append(df.iloc[r][c])
        if r==2+r*1 and c==0:
            address.append(df.iloc[r][c])
        if r==3+r*1 and c==0:
            address2.append(df.iloc[r][c])
        if r==4+r*1 and c==0:
            city.append(df.iloc[r][c])
        if r==5+r*1 and c==0:
            state.append(df.iloc[r][c])
        if r==5+r*1 and c==1:
            zipcode.append(df.iloc[r][c])
        if r==0+r*1 and c==3:
            occupation.append(df.iloc[r][c])
print(len(first_name))
print(len(date))
print(len(last_name))
print(len(cash_amount))
print(len(address))
print(len(address2))
print(len(occupation))
​

Results:

5
5
0
5
0
0
5

Aucun commentaire:

Enregistrer un commentaire