I have the following pandas table in Python
df1 = pd.DataFrame(np.array([['Jim', 1, 1, 1, 0], ['Jack', 1, 0, 0, 1], ['Joe', 0, 1, 1, 1]]),
columns=['name', 'slot1', 'slot2', 'slot3', 'slot4'])
And I want to add a new column which contains the names of the columns when the value in each column is 0.
The desired outcome should be the following.
df2 = pd.DataFrame(np.array([['Jim', 1, 1, 1, 0, 'slot4'], ['Jack', 1, 0, 0, 1, 'slot2,slot3'], ['Joe', 0, 1, 1, 1, 'slot1']]),
columns=['name', 'slot1', 'slot2', 'slot3', 'slot4', 'error'])
I tried the following codes but they don't give the desired outcome:
df1['error'] = df1.loc[:,"slot1":"slot4"].apply(lambda row: row[row == 0].index, axis=1)
df1['error'] = df1.loc[:,"slot1":"slot4"].isin(["0"]).idxmax(1)
Could you please suggest a solution on this task? Thank you in advance.