Basically I have a list of questions from a survey '''list_of_columns''' that also serve as columns in my dataframe. In the beginning of my code I replaced all blank responses as 'Empty', but some survey respondents did not answer questions they were supposed to (as indicated by whether or not a '1' is marked in the 'EOPS/CARE' or 'CalWORKs' columns of my dataframe, but have 'Empty' in a question pertaining to those respective programs), so I want to recode 'Empty's as 'Missing's in these cases to accurately reflect that.
Here is the code I have to try and remedy that:
list_of_columns = ['E1', 'E2', 'E3', 'E5', 'E11', 'E13', 'E14', 'E17', 'E18', 'E20', 'C2', 'C7', 'C8', 'C9', 'C11', 'C12', 'NU2', 'NU7', 'NU8', 'NU10', 'NU11', 'CAL1', 'CAL2', 'CAL3', 'CAL5', 'CAL10', 'CAL12', 'CAL14', 'CAL15', 'O1'] # list of survey questions that are also columns in my df. Questions with 'E' indicate they are related to EOPS/CARE, questions with 'CAL' indicated they are related to CalWORKs, etc.
for question in list_of_columns:
if 'E' in question and data_final['EOPS/CARE'] == 1: # if 'E' is in the question, and the column 'EOPS/CARE' in my df is equal to 1, replace all instances of "Empty" with "Missing"
data_final[question] = np.where(data_final[question] == "Empty", "Missing", data_final[question])
elif 'CAL' in question and data_final['CalWORKs'] == 1: # similarly, if 'CAL' is in the question, and the column 'EOPS/CARE' in my df is equal to 1, replace all instances of "Empty" with "Missing"
data_final[question] = np.where(data_final[question] == "Empty", "Missing", data_final[question])
else:
pass
I keep getting this when I try to execute: "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
This would work fairly easily in Stata, but I'm determined to do this in Python as the rest of my code is already in Python. I'm still learning the language so it could be due to syntax. Thanks so much!