0

This loop is working to change other columns in my DataFrame. But I keep running into this error on this specific column/dataframe. I'm basically trying do in a VLOOKUP between tables in Pandas.

KeyError: 'cannot use a single bool to index into setitem'

This code is working fine with no errors. It returns the appropiate value to the "Adjusted Agency" column

for i in range(len(df_ult_cust)):

    df.loc[df.Ult_Cust == df_ult_cust['Ult_Cust'].iloc[i], 'Adjusted Agency'] = df_ult_cust['ACRONYM'].iloc[i]

But when I use this block of code, I get that strange key error listed above.

for j in range(len(df_pca_cust)):

    df.loc[df.div == df_pca_cust['pca'].iloc[j], 'Division'] = df_pca_cust['div_acronym'].iloc[j]

Note: I chose not to use the itterows Pandas function here as for some reason it was causing the code to run slower.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
dan
  • 62
  • 8
  • 1
    If you are essentially trying to do a vlookup just use `merge` [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – It_is_Chris Jan 26 '22 at 17:41
  • 1
    if `div` is meant to be a column in your DataFrame, maybe there's an issue with a clash with the DataFrame method `df.div()`. more broadly, i avoid using "dot" access to columns due to potential problems such as this. – mechanical_meat Jan 26 '22 at 17:43
  • 1
    That's a great point `df['div']` may work. @mechanical_meat – Henry Ecker Jan 26 '22 at 17:44
  • 1
    @mechanical_meat you were right! That was the problem. Thank you so much for your help. Thank you too Henry Ecker and It_is_Chris – dan Jan 26 '22 at 17:51

0 Answers0