Home > database >  how to make a subset of rows of a with a list of values
how to make a subset of rows of a with a list of values

Time:01-06

when I try to make a subset of rows of a df, I use this code

lst_of_brands =['brand_1','brand_2','Brand_3']


df_selected_brands = df_all_products.loc[df_all_products['brands'].isin(lst_of_brands)]

when I iterate over a column in the new df it stops at key nr 10

for item in range(len(df_selected_brands)):
    product_id = df_selected_brands['id'][item]
    print (product_id)

This code stops at key nr 10 and gives error

KeyError:10

Iterating over same colum in original df is no problem.

I think there's something wrong with the way I make the subset but can't figure out what.

CodePudding user response:

Reset the index of df_selected_brands before looping:

df_selected_brands.reset_index(drop=True, inplace=True)

for item in range(len(df_selected_brands)):
    product_id = df_selected_brands['id'][item]
    print (product_id)

When subseting a dataframe indices retain their original value. So when you iterate over a dataframe or series using indices, they can't align as expected and a key error is raised.

CodePudding user response:

If you really want to iterate over each row by position, use .iloc:

for item in range(len(df_selected_brands)):
    product_id = df_selected_brands['id'].iloc[item]  # <- HERE
    print (product_id)

but you can also use itertuples:

for item in df_selected_brands.itertuples():
    product_id = item.Id
    print(product_id)
  • Related