Rahul Sharma
Rahul Sharma

Reputation: 2495

TypeError: 'int' object is not subscriptable in dataframe.iterrows

I am trying to loop over a dataframe like the following:

for row, index in split[0].iterrows():
        kitname = row['kit_name'][0]
        print(kitname)

where split is a list of dataframes

split[0] :

  kit_name          kit_info         part_name part_number  
0  KIT0001  Standard FLC Kit  Standard FLC Kit           0   
1  KIT0001  Standard FLC Kit  Standard FLC Kit           0   
2  KIT0001  Standard FLC Kit  Standard FLC Kit           0   

but the following error is coming:

TypeError: 'int' object is not subscriptable

I am using the same code in a different script and it's working fine there

Upvotes: 1

Views: 702

Answers (1)

jezrael
jezrael

Reputation: 862781

Problem is you swap index and row variables, so row are integers so select ['kit_name'] failed:

for row, index in split[0].iterrows():
    kitname = row['kit_name'][0]
    print(kitname)

Need:

for index, row in split[0].iterrows():
    kitname = row['kit_name'][0]
    print(kitname)

Upvotes: 3

Related Questions