Coding Ref

How to find the minimum in Pandas

How to find the minimum in Pandas

In Pandas, the min method is used to return the minimum value in a DataFrame. This method will return the minimum value in each column of the DataFrame, unless a specific column is specified.

Example

For example, consider the following DataFrame:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

This DataFrame has three columns A, B, and C, with five rows of data.

To find the minimum value in each column of this DataFrame, you could use the following code:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Find the minimum value in each column
min_values = df.min()

# Print the minimum values
print(min_values)
output
A      1
B     10
C    100
dtype: int64

In the code above, the min method is applied to the DataFrame, without specifying a specific column.

This returns a Series object containing the minimum value in each column. In this case, the minimum values are 1, 10, and 100 for columns A, B, and C, respectively.

Find the min of a column

You can also use the min method to find the minimum value in a specific column of the DataFrame.

For example, to find the minimum value in column B, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Find the minimum value in column B
min_value = df['B'].min()

# Print the minimum value
print(min_value)

In the code above, the min method is applied to the B column of the DataFrame. This returns the minimum value in that column, which is 10 in this case.

Find the min of multiple columns

You can also use the min method to find the minimum value in multiple columns of the DataFrame.

For example, if you wanted to find the minimum value in either column B or column C, you could do the following:

main.py
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4, 5],
    'B': [10, 20, 30, 40, 50],
    'C': [100, 200, 300, 400, 500]
})

# Find the minimum value in columns B and C
min_value = df[['A', 'B']].min().min()

# Print the minimum value
print(min_value)

In the code above, the min method is applied to the A and B columns of the DataFrame.

This returns the minimum value in either of these two columns, which is 1 in this case.

You'll also like

Related tutorials curated for you

    How to find the mode in a Pandas DataFrame

    How to use Timedelta in Pandas

    How to read a TSV file in Pandas

    How to use ewm() in Pandas

    How to join two DataFrames in Pandas

    How to reshape a Pandas DataFrame

    What is idxmax() in Pandas?

    What does Diff() do in Pandas?

    Pandas read SQL

    How to fix: AttributeError module 'pandas' has no attribute 'dataframe'

    How to groupby mean in Pnadas

    How to sort a series in Pandas