0

The train is a data frame in which I have two columns:

  Address   gender
0 abc        f
1 dfg        m
2 hjk        m
3 ert        m

when genders are the same I need to perform some operation on the address column so I am using this code moreover I am new to python and pandas so I don't know if I am doing it correctly and efficiently.


for i in train.index:
    for j in train.index[i+1]:
        
        if train.loc[i,'gender']== train.loc[j,'gender']:
            print(train['Address'])

currently, this code is giving an error:

'int' object is not iterable

can anyone please help me solve this issue?

peeps
  • 43
  • 7

1 Answers1

1

The error that you are having is because the train.index[i+1] is not interable. It is an integer.

Why you don't use .shift instead using the second loop?

for i in train.index:
    if train.loc[i,'gender']== train['gender'].shift(-1)[i]:
        print(train['Address'])

Just to remember, the line print(train['Address']) will print all the 'Address' Column. If you want the specific 'Address' when your conditional is True you should use print(train['Address'][i])

Romero_91
  • 405
  • 3
  • 15