computing the minimum value for each row in a pandas dataframe [duplicate]

  • Last Update :
  • Techknowledgy :

To find minimum value of every row in DataFrame just call the min() member function with DataFrame object with argument axis=1 i.e. ,To find minimum value of every column in DataFrame just call the min() member function with DataFrame object without any argument i.e. ,We got the minimum value of each column or row, but what if we want to know the exact index position in every column or row where this minimum value exists ? To get the index of minimum value of elements in row and columns, pandas library provides a function i.e. ,As we can see that it has skipped the NaN while finding the min value. We can include the NaN too if we want i.e.

Python’s Pandas Library provides a member function in Dataframe to find the minimum value along the axis i.e.

DataFrame.min(axis = None, skipna = None, level = None, numeric_only = None, ** kwargs)

Suppose we have a Dataframe i.e.

# List of Tuples
matrix = [(22, 16, 23),
   (33, np.NaN, 11),
   (44, 34, 11),
   (55, 35, np.NaN),
   (66, 36, 13)
]

# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index = list('abcde'), columns = list('xyz'))

    x y z
    a 22 16.0 23.0
    b 33 NaN 11.0
    c 44 34.0 11.0
    d 55 35.0 NaN
    e 66 36.0 13.0

To find minimum value of every row in DataFrame just call the min() member function with DataFrame object with argument axis=1 i.e.

# Get a series containing minimum value of each row
minValuesObj = dfObj.min(axis = 1)

print('minimum value in each row : ')
print(minValuesObj)

# Get a series containing minimum value of each column without skipping NaN
minValuesObj = dfObj.min(skipna = False)

print('minimum value in each column including NaN: ')
print(minValuesObj)

Suggestion : 2

Use pandas.DataFrame.min with axis=1:

df['Dmin'] = df.min(axis = 1)

Suggestion : 3

Last Updated : 02 Jul, 2020

Output:

20

Suggestion : 4

pandas.DataFrame.min , pandas.DataFrame.max , pandas.DataFrame.mad , pandas.DataFrame.mod

>>> idx = pd.MultiIndex.from_arrays([
         ...['warm', 'warm', 'cold', 'cold'],
         ...['dog', 'falcon', 'fish', 'spider']
      ],
      ...names = ['blooded', 'animal']) >>>
   s = pd.Series([4, 2, 0, 8], name = 'legs', index = idx) >>>
   s
blooded animal
warm dog 4
falcon 2
cold fish 0
spider 8
Name: legs, dtype: int64
>>> s.min()
0