对Pandas数据框架的行进行排序

对Pandas数据框架的行进行排序

要对Pandas DataFrame的行进行排序,我们可以使用DataFrame.rank()方法,该方法返回所传递的系列的每个索引的排序。排名是在排序后的位置基础上返回的。

例子 #1 :

在这里,我们将创建一个电影的DataFrame,并根据它们的评分进行排名。

# import the required packages 
import pandas as pd 
  
# Define the dictionary for converting to dataframe 
movies = {'Name': ['The Godfather', 'Bird Box', 'Fight Club'],
         'Year': ['1972', '2018', '1999'],
         'Rating': ['9.2', '6.8', '8.8']}
  
df = pd.DataFrame(movies)
print(df)

输出:
对Pandas数据框架的行进行排序

# Create a column Rating_Rank which contains
# the rank of each movie based on rating
df['Rating_Rank'] = df['Rating'].rank(ascending = 1)
  
# Set the index to newly created column, Rating_Rank
df = df.set_index('Rating_Rank')
print(df)

输出:
对Pandas数据框架的行进行排序

# Sort the dataFrame based on the index
df = df.sort_index()
print(df)

输出:
对Pandas数据框架的行进行排序

示例 #2
让我们以4名学生的分数为例。我们将根据学生们的最高分数来进行排名。

# Create a dictionary with student details
student_details = {'Name':['Raj', 'Raj', 'Raj', 'Aravind', 'Aravind', 'Aravind',
                             'John', 'John', 'John', 'Arjun', 'Arjun', 'Arjun'],
                   'Subject':['Maths', 'Physics', 'Chemistry', 'Maths', 'Physics',
                             'Chemistry', 'Maths', 'Physics', 'Chemistry', 'Maths',
                                                           'Physics', 'Chemistry'],
                   'Marks':[80, 90, 75, 60, 40, 60, 80, 55, 100, 90, 75, 70]
               }
  
# Convert dictionary to a DataFrame
df = pd.DataFrame(student_details)
print(df)

输出:
对Pandas数据框架的行进行排序

# Create a new column with Marks 
# ranked in descending order
df['Mark_Rank'] = df['Marks'].rank(ascending = 0)
  
# Set index to newly created column 
df = df.set_index('Mark_Rank')
print(df)

输出:
对Pandas数据框架的行进行排序

# Sort the DataFrame based on the index 
df = df.sort_index()
  
print(df)

输出:
对Pandas数据框架的行进行排序

解释:
请注意,拉吉和阿琼各得90分,因此他们的排名是2.5(第二和第三名的平均值,即他们共享的两个排名)。这一点在表中的其他分数中也可以看到。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程