我有需要檢查條件的資料框df和列串列cols_list。
前任: cols_list = [cols1, cols2, col3]
我需要創建新列,如下所示,
df['new_col'] = np.where((df['cols1'] == 1) | (df['cols1'] == 1) | (df['cols1'] == 1), 1, 0)
我有多個cols_list不同長度的,如何(df['cols1'] == 1) | (df['cols1'] == 1) | (df['cols1'] == 1)為多個迭代創建這個條件cols_list?
目前,我需要手動完成,因為cols_list有數百種,我正在尋找更好的方法!
uj5u.com熱心網友回復:
用:
np.where((df[cols_list].eq(1).any(axis=1), 1, 0))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/344595.html
