I want to write a function that can look at a dataframe, find the max or min value in a specified column, then return the entire datafrane with the row(s) containing the max or min value at the bottom.
I have made it so that the rows with the max or min value alone get returned.
def findAggregate(df, transType, columnName=None):
if transType == 'max1Column':
return df[df[columnName] == df[columnName].max()]
elif transType == 'min1Column':
return df[df[columnName] == df[columnName].min()]
Given the dataframe below, I want to check col2 for the MIN value
Original Dataframe:
col1 col2 col3
blue 2 dog
orange 18 cat
black 6 fish
Expected output:
col1 col2 col3
blue 2 dog
orange 18 cat
black 6 fish
blue 2 dog
Actual output:
col1 col2 col3
blue 2 dog
And use one loc
i = df.col2.idxmin()
df.loc[[*df.index] + [i]]
col1 col2 col3
0 blue 2 dog
1 orange 18 cat
2 black 6 fish
0 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 col3
0 blue 2 dog
1 orange 18 cat
2 black 6 fish
0 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 col3
0 blue 2 dog
1 orange 18 cat
2 black 6 fish
3 blue 2 dog
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With