2021年3月15日 星期一

[ Python 常見問題 ] Pandas - How to find a minimum value in a pandas dataframe column ?

 Source From Here

Question
Examples of how to find a minimum value in a pandas dataframe column.

HowTo

Create a dataframe
Lets create for example a simple data frame:
  1. import pandas as pd  
  2.   
  3. data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve','Becky','Bob'],   
  4.         'Age':[36,27,20,12,30,20,22,21]}  
  5.   
  6. df = pd.DataFrame(data)  
  7. df  
Output:
  1.     Name  Age  
  2. 0    Ben   36  
  3. 1   Anna   27  
  4. 2    Zoe   20  
  5. 3    Tom   12  
  6. 4   John   30  
  7. 5  Steve   20  
  8. 6  Becky   22  
  9. 7    Bob   21  
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:
  1. df['Age'].min()  # Output: 12  
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:
  1. df['Age'].idxmin()  # Output: 3  
Then using the index above:
Output:
  1. Name    Tom  
  2. Age      12  
  3. Name: 3, dtype: object  
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:
  1. import pandas as pd  
  2.   
  3. data = {'Name':['Ben','Anna','Zoe','Tom','John','Steve','Becky','Bob'],   
  4.         'Age':[12,27,20,12,30,20,22,21]}  
  5.   
  6. df = pd.DataFrame(data)  
  7.   
  8. print(df)  
Output:
  1.     Name  Age  
  2. 0    Ben   12  
  3. 1   Anna   27  
  4. 2    Zoe   20  
  5. 3    Tom   12  
  6. 4   John   30  
  7. 5  Steve   20  
  8. 6  Becky   22  
  9. 7    Bob   21  
Then the function min:
  1. df['Age'].min()  # Output: 12  
however idxmin:
  1. df['Age'].idxmin()  # Output: 0  
To get rows with a min value in the column Age a solution is to do:
  1. df[ df['Age'] == df['Age'].min() ]  
Output:
  1.   Name  Age  
  2. 0  Ben   12  
  3. 3  Tom   12  
and to get the indexes:
  1. df[ df['Age'] == df['Age'].min() ].index  
which return:
  1. Int64Index([03], dtype='int64')  


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...