【问题标题】:Creating if statement with multiple conditions involving specific df column and string character in list创建具有多个条件的 if 语句,这些条件涉及列表中的特定 df 列和字符串字符
【发布时间】:2020-04-22 23:56:56
【问题描述】:

基本上,我有一个来自调查“list_of_columns”的问题列表,这些问题也用作我的数据框中的列。在我的代码开头,我将所有空白回复替换为“空”,但一些调查受访者没有回答他们应该回答的问题(如“EOPS/CARE”或“EOPS/CARE”中是否标记“1”所示)我的数据框的 CalWORKs 列,但在与这些相应程序有关的问题中有 'Empty'),所以我想在这些情况下将 'Empty' 重新编码为 'Missing' 以准确反映这一点。

这是我必须尝试解决的代码:

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

当我尝试执行时,我不断收到此消息:“ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.全部()。”

这在 Stata 中工作起来相当容易,但我决定在 Python 中执行此操作,因为我的其余代码已经在 Python 中。我仍在学习该语言,因此可能是由于语法。非常感谢!

【问题讨论】:

  • 到底是什么问题?你读过 Pandas 文档吗?
  • 嗨@AMC,代码由于某种原因无法正常工作。我尝试执行时得到的错误是“ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 。”
  • 这是整个错误信息吗?您是否尝试过对该错误进行一些研究?我知道我个人最近几次在这里看到过同样的问题。无论如何,这里不足以重现错误,所以我所能做的就是将您引导至文档和其他问题。
  • 在我看来,这应该作为stackoverflow.com/q/36921951/11301900的副本关闭。
  • 感谢您的链接。我尝试了这些,但即使我使用按位 '&' 替换我的 'and',我也会收到一个新错误:“TypeError: cannot compare a dtyped [int64] array with a scalar of type [bool]”,问题行是第 5 行(“如果 'E' 有问题 & data_final['EOPS/CARE']...”我束手无策!

标签: python pandas for-loop if-statement replace


【解决方案1】:

这只是将列移至所需位置的快速方法。

# as indicated in your question list_of_columns is also a column in df

df.loc[(df['list_of_columns'].str.contains('E')) & (df['EOPS/CARE'] == 1) & (df['Column name where empty would be present'] == 'Empty'),'Column Name where Empty would be present'] = 'Missing'

做同样的事情来让另一个条件起作用。我无法理解问题的其余部分,但如果你澄清我可以提供进一步的帮助。

.loc 将为您提供最大的帮助。检查文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多