Source From Here
Preface
Below will provide examples of how to find a minimum value in a pandas Dataframe's column:
HowTo
Create a dataframe
Lets create for example a simple dataframe:
- test.py
which returns:
Find the min value in the column Age
To find the minimum value in the column Age, a solution is to use the pandas function min:
Find the index corresponding to the min value in the column Age
It is also possible to find the index corresponding to the min value in the column Age using the pandas function called idxmin:
Then using the index above:
An example with multiple rows with a min value in the same column
Lets create a dataframe with two min values in the column Age:
Then the function min:
However, the idxmin will only return only the first one with minimum value:
To get all row(s) with a min value in the column Age, a solution is to do:
and to get the indexes:
Below will provide examples of how to find a minimum value in a pandas Dataframe's column:
HowTo
Create a dataframe
Lets create for example a simple dataframe:
- test.py
- import pandas as pd
- data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve','Becky','Bob'],
- 'Age':[36,27,20,12,30,20,22,21]}
- df = pd.DataFrame(data)
- print(f"{df}")
- Name Age
- 0 Ben 36
- 1 Anna 27
- 2 Zoe 20
- 3 Tom 12
- 4 John 30
- 5 Steve 20
- 6 Becky 22
- 7 Bob 21
To find the minimum value in the column Age, a solution is to use the pandas function min:
Find the index corresponding to the min value in the column Age
It is also possible to find the index corresponding to the min value in the column Age using the pandas function called idxmin:
Then using the index above:
An example with multiple rows with a min value in the same column
Lets create a dataframe with two min values in the column Age:
Then the function min:
However, the idxmin will only return only the first one with minimum value:
To get all row(s) with a min value in the column Age, a solution is to do:
and to get the indexes:
沒有留言:
張貼留言