【发布时间】:2016-12-30 08:44:39
【问题描述】:
我一直被熊猫问题困住,我似乎无法弄清楚。 我有一个这样的数据框:
ref, value, rule, result, new_column
a100, 25, high, fail, nan
a100, 25, high, pass, nan
a100, 25, medium, fail, nan
a100, 25, medium, pass, nan
a101, 15, high, fail, nan
a101, 15, high, pass, nan
a102, 20, high, pass, nan
我想使用以下伪代码向此数据框添加一个新列
对于 ref 中的每个唯一值,如果为 result = fail,则对于具有相同“ref”值的所有后续行,则为 new_column = no。
这就是新数据框的外观。
ref, value, rule, result, new_column
a100, 25, high, fail, no
a100, 25, high, pass, no
a100, 25, medium, fail, no
a100, 25, medium, pass, no
a101, 15, high, fail, no
a101, 15, high, pass, no
a102, 20, high, pass, yes
我已经设法做到以下几点:
ref, value, rule, result, new_column
a100, 25, high, fail, no
a100, 25, high, pass, yes
这是通过df.loc 函数实现的。
但我需要将函数应用于唯一值,而不是每一行。
【问题讨论】:
-
你觉得
new_column = no还是yes? -
你能在数据框中添加更多行吗,因为我觉得这对我来说有点不清楚。
-
@jezrael 已更新,请检查。对于结果 = 失败的所有实例,对于每个唯一的 ref 值,new_column = no。
-
@Kvothe 如果你的前两行被交换,new_column 是yes、no、no、no 还是仍然是no?
-
@JonClements,我已经更新了这个问题。但如果行被交换。对于“ref”的值,它仍然是不,不,不。
标签: python pandas dataframe unique conditional-statements