I have a dataframe:
df = pd.DataFrame({'A': [0, 0, 1], 'B': [1, 0, 0]}, index=['x', 'y', 'z'])
A B
x 0 1
y 0 0
z 1 0
For each row, I want the names of all the columns with the lowest value (edit: per row), something like:
x A
y A
y B
z B
# or
x [A]
y [A, B]
z [B]
I know idxmin() gives the first instance of the lowest value:
df.idxmin(axis=1)
x A
y A
z B
But what is an efficient way to get all of them?
This question gives all of the rows with the minimum value in a specific column, but that's not quite what I want.
Edit: Here's a better toy df to play with for getting the column names with the minimum value in each row:
df2 = pd.DataFrame({'A': [1, 0, 6], 'B': [3, 0, 2]}, index=['x', 'y', 'z'])
A B
x 1 3
y 0 0
z 6 2
df.idxmin