慕婉清6462132
你可以用pandas.DataFrame.any()与axis=1:listVars=['var1', 'var2', 'var3']myDF['newVar'] = np.where((myDF[listVars]==1).any(axis=1), 1, 0)例如:myDF = pd.DataFrame({ "var1": [1, 1, 1, 0, 0], "var2": [1, 0, 1, 0, 1], "var3": [0, 0, 0, 0, 0]})listVars=['var1', 'var2', 'var3']myDF['newVar'] = np.where((myDF[listVars]==1).any(1),1, 0)print(myDF)# var1 var2 var3 newVar#0 1 1 0 1#1 1 0 0 1#2 1 1 0 1#3 0 0 0 0#4 0 1 0 1