我一直被困在熊貓的問題上,我似乎無法弄清楚。 我有這樣一個數據幀:熊貓將函數應用於列中的唯一值
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
我想一個新列添加到該數據框與下面的僞代碼
對於裁判的每個唯一值,如果result = fail
,然後new_column = no
爲所有後續行相同的「ref」值。
這就是新數據框的外觀。
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
您是否可以向數據框添加更多行,因爲我認爲這對我來說有點不清楚。 – jezrael
@jezrael更新,請檢查。對於每個唯一參考值,result = fail的所有實例均爲new_column = no。 – Kvothe