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

Time:04-01

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

CodePudding user response:

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)
  • Related