0

I a trying to write a program in Python and I am getting the following error:

raise ValueError("Cannot index with multidimensional key")
ValueError: Cannot index with multidimensional key

The error occurs when I call this function:

def pd_remove_condition(data,row,conditions):

for i in range(len(conditions)):
    data = data.loc[data[row] != conditions[i]]

return data

This function is called several times and only errors on one occasion. The data is dataframe that is imported from a simple csv. I have "Significant Smoker", "Current Smoker" and "Heavy Drinker" columns that I use to calculate the column "Heavy Smoker or Drinker":

adjusted_data["Smoker"] = np.where(adjusted_data["Significant Smoker"]=="No","Not at all",adjusted_data["Current Smoker"])
adjusted_data["Smoker"] = np.where(np.logical_or(adjusted_data["Smoker"]=="Not asked or Missing",adjusted_data["Smoker"]=="Don´t Know/Not Sure"),"Unknown", adjusted_data["Smoker"])
adjusted_data["Smoker"] = np.where(adjusted_data["Smoker"]=="Refused","Unknown", adjusted_data["Smoker"])
adjusted_data["Heavy Smoker or Drinker"] = np.where(np.logical_or(adjusted_data["Smoker"]=="Every day",adjusted_data["Heavy Drinker"]==2),True, False)
adjusted_data["Heavy Smoker or Drinker"] = np.where(np.logical_and(adjusted_data["Smoker"]!="Every day",adjusted_data["Heavy Drinker"]==9),"Unknown",adjusted_data["Heavy Smoker or Drinker"] )
adjusted_data["Heavy Smoker or Drinker"] = np.where(np.logical_and(adjusted_data["Smoker"]=="Unknown",adjusted_data["Heavy Drinker"]!=2),"Unknown",adjusted_data["Heavy Smoker or Drinker"] )

When I try to run pd_remove_condition, I am only trying to remove records with "Unknown" in the "Heavy Smoker or Drinker" Column, i.e. row = "Heavy Smoker or Drinker" and conditions = ["Unknown"].

Since the function works properly in other circumstances, it makes me think that the issue is with the creation of the ["Heavy Smoker or Drinker"] column above. In creating it, perhaps I have accidentally made a multi-dimensional column? I'm a Python noob and I'm kind of stumped as how to solve this. All help is appreciated!

Kind regards,

C

EDIT:

Here is a copy of the column once created when using.to_csv:

Heavy Smoker or Drinker
False
False
False
False
False
True
False
False
False
False
False
Unknown
False
False
False
False
False

I am trying to workout why feeding this through pd_remove_condition results in an error rather than removing the "unknowns"

Charmalade
  • 645
  • 1
  • 6
  • 14
  • To get help faster, give you sample data and expected output. People tend not to read code plus there is always multiple ways to reach a desired outcome. – wwnde May 27 '21 at 21:22
  • When you [caught the error](https://docs.python.org/3/tutorial/errors.html#handling-exceptions) and inspected/printed relevant data in the except suite - was there anything obviously wrong? – wwii May 27 '21 at 21:22
  • If you are using an IDE **now** is a good time to learn its debugging features. Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii May 27 '21 at 21:25

0 Answers0