Return rows with max/min values at bottom of dataframe (python/pandas) Return rows with max/min values at bottom of dataframe (python/pandas) pandas pandas

Return rows with max/min values at bottom of dataframe (python/pandas)


Focus on the index values

And use one loc

i = df.col2.idxmin()df.loc[[*df.index] + [i]]     col1  col2  col30    blue     2   dog1  orange    18   cat2   black     6  fish0    blue     2   dog

Same idea but with Numpy and iloc

i = np.arange(len(df))a = df.col2.to_numpy().argmin()df.iloc[np.append(i, a)]     col1  col2  col30    blue     2   dog1  orange    18   cat2   black     6  fish0    blue     2   dog


Use idxmin or idxmax:

edited to .loc after AndyL's comment

df.append(df.loc[df['col2'].idxmin()], ignore_index=True)     col1  col2  col30    blue     2   dog1  orange    18   cat2   black     6  fish3    blue     2   dog


You can do this in oneliner:

df.append(df.loc[df['col2'].idxmin()])

Output:

     col1  col2  col30    blue     2   dog1  orange    18   cat2   black     6  fish0    blue     2   dog