Pandas数据框架中某一列的百分位数排名

Pandas数据框架中某一列的百分位数排名

让我们看看如何在Pandas DataFrame中找到一个列的百分位数排名。我们将使用rank()函数,参数pct = True来查找百分位数排名。

例子1 :

# import the module
import pandas as pd 
  
# create a DataFrame 
data = {'Name': ['Mukul', 'Rohan', 'Mayank', 
                 'Shubham', 'Aakash'],
        'Location' : ['Saharanpur', 'Meerut', 'Agra', 
                      'Saharanpur', 'Meerut'],
        'Pay' : [50000, 70000, 62000, 67000, 56000]} 
df = pd.DataFrame(data)  
  
# create a new column of percentile rank
df['Percentile Rank'] = df.Pay.rank(pct = True)
  
# displaying the percentile rank
display(df) 
Python

输出 :
Pandas数据框架中某一列的百分位数排名

例子2 :

# import the module
import pandas as pd 
  
# create a DataFrame 
ODI_runs = {'name': ['Tendulkar', 'Sangakkara', 'Ponting', 
                      'Jayasurya', 'Jayawardene', 'Kohli', 
                      'Haq', 'Kallis', 'Ganguly', 'Dravid'], 
            'runs': [18426, 14234, 13704, 13430, 12650, 
                     11867, 11739, 11579, 11363, 10889]} 
df = pd.DataFrame(ODI_runs)  
  
# create a new column of percentile rank
df['Percentile Rank'] = df.runs.rank(pct = True)
  
# displaying the percentile rank
display(df) 
Python

输出 :
Pandas数据框架中某一列的百分位数排名

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册