Get minimum values in rows or columns with their index position in Pandas-Dataframe
Let’s discuss how to find minimum values in rows & columns of a Dataframe and also their index position.
a) Find the minimum value among rows and columns :
Dataframe.min() : This function returns the minimum of the values in the given object. If the input is a series, the method will return a scalar which will be the minimum of the values in the series. If the input is a dataframe, then the method will return a series with a minimum of values over the specified axis in the dataframe. By default, the axis is the index axis.
1) Get minimum values of every column :
Use min()
function to find the minimum value over the index axis.
Code :
import pandas as pd
data = [
( 20 , 16 , 23 ),
( 30 , None , 11 ),
( 40 , 34 , 11 ),
( 50 , 35 , None ),
( 60 , 40 , 13 )
]
df = pd.DataFrame(data, index = [ 'a' , 'b' , 'c' , 'd' , 'e' ],
columns = [ 'x' , 'y' , 'z' ])
minvalue_series = df. min ()
minvalue_series
|
Output:
2) Get minimum values of every row :
Use min()
function on a dataframe with ‘axis = 1’ attribute to find the minimum value over the row axis.
Code :
import pandas as pd
data = [
( 20 , 16 , 23 ),
( 30 , None , 11 ),
( 40 , 34 , 11 ),
( 50 , 35 , None ),
( 60 , 40 , 13 )
]
df = pd.DataFrame(data, index = [ 'a' , 'b' , 'c' , 'd' , 'e' ],
columns = [ 'x' , 'y' , 'z' ])
minvalue_series = df. min (axis = 1 )
minvalue_series
|
Output:
3) Get minimum values of every column without skipping None Value :
Use min()
function on a dataframe which has Na value with ‘skipna = False’ attribute to find the minimum value over the column axis.
Code :
import pandas as pd
data = [
( 20 , 16 , 23 ),
( 30 , None , 11 ),
( 40 , 34 , 11 ),
( 50 , 35 , None ),
( 60 , 40 , 13 )
]
df = pd.DataFrame(data, index = [ 'a' , 'b' , 'c' , 'd' , 'e' ],
columns = [ 'x' , 'y' , 'z' ])
minvalue_series = df. min (skipna = False )
minvalue_series
|
Output:
4) Get minimum value of a single column :
Use min()
function on a series to find the minimum value in the series.
Code :
import pandas as pd
data = [
( 20 , 16 , 23 ),
( 30 , None , 11 ),
( 40 , 34 , 11 ),
( 50 , 35 , None ),
( 60 , 40 , 13 )
]
df = pd.DataFrame(data, index = [ 'a' , 'b' , 'c' , 'd' , 'e' ],
columns = [ 'x' , 'y' , 'z' ])
minvalue = df[ 'x' ]. min ()
minvalue
|
Output:
20
b) Get row index label or position of minimum values among rows and columns :
Dataframe.idxmin() : This function returns index of first occurrence of minimum over requested axis. While finding the index of the minimum value across any index, all NA/null values are excluded.
1) Get row index label of minimum value in every column :
Use idxmin()
function to find the index/label of the minimum value along the index axis.
Code :
import pandas as pd
data = [
( 20 , 16 , 23 ),
( 30 , None , 11 ),
( 40 , 34 , 11 ),
( 50 , 35 , None ),
( 60 , 40 , 13 )
]
df = pd.DataFrame(data, index = [ 'a' , 'b' , 'c' , 'd' , 'e' ],
columns = [ 'x' , 'y' , 'z' ])
minvalueIndexLabel = df.idxmin()
minvalueIndexLabel
|
Output
2) Get Column names of minimum value in every row :
Use idxmin()
function with ‘axis = 1’ attribute to find the index/label of the minimum value along the column axis.
Code :
import pandas as pd
data = [
( 20 , 16 , 23 ),
( 30 , None , 11 ),
( 40 , 34 , 11 ),
( 50 , 35 , None ),
( 60 , 40 , 13 )
]
df = pd.DataFrame(data, index = [ 'a' , 'b' , 'c' , 'd' , 'e' ],
columns = [ 'x' , 'y' , 'z' ])
minvalueIndexLabel = df.idxmin(axis = 1 )
minvalueIndexLabel
|
Output
Last Updated :
02 Jul, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...