How Do I Find Where A Maximum Value Lives in a DataFrame?

Say we’ve created the following dataframe:

df = pd.DataFrame(np.random.randint(0,100, size=(10,4)), columns=list('ABCD'))

Now we want to accomplish two things. We want to loop through all the rows and find which column contains the largest (or smallest) integer for each row, and we want to loop through the columns to find the row containing the largest (or smallest) integer.

Looping through rows to find the column where the largest integer lives:

for idx in df.index:
    print(df.loc[idx].idxmax())

The same thing for the smallest integer:

for idx in df.index:
    print(df.loc[idx].idxmin())

Looping over all the columns to find the row where the largest integer occurs:

for col in df.columns:
    print(df.loc[:, col].idxmax())

The same thing for the smallest integer:

for col in df.columns:
    print(df.loc[:, col].idxmin())