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"