Pandas sorting skills revealed: How to sort according to specific conditions requires specific code examples
In the process of data processing and analysis, sorting is a very common task operate. The Pandas library is one of the powerful tools for data analysis in Python. It provides rich sorting functions that can sort data according to specific conditions. This article will introduce several commonly used sorting techniques and provide specific code examples.
1. Sort by a single column
First, let’s look at how to sort by a single column. The sort_values()
function in Pandas can sort DataFrame or Series objects. Below is an example data set, we will sort by the "score" column in descending order:
import pandas as pd data = {'name': ['Alice', 'Bob', 'Tom', 'Jerry'], 'score': [90, 80, 95, 85], 'age': [25, 30, 27, 23]} df = pd.DataFrame(data) df_sorted = df.sort_values(by='score', ascending=False) print(df_sorted)
Output results:
name score age 2 Tom 95 27 0 Alice 90 25 3 Jerry 85 23 1 Bob 80 30
In the above code, we use sort_values()
Function and set the parameter by
to the column name to be sorted. In addition, ascending=False
means descending sorting. If you want to sort in ascending order, set it to ascending=True
.
2. Sort by multiple columns
In addition to sorting by single column, we can also sort by multiple columns. When there are multiple sorting conditions, you can use the by
parameter of the sort_values()
function to pass in a list containing multiple column names. The following example will be sorted in descending order according to the "score" column. If the "score" columns are the same, then sorted in ascending order according to the "age" column:
import pandas as pd data = {'name': ['Alice', 'Bob', 'Tom', 'Jerry'], 'score': [90, 80, 95, 85], 'age': [25, 30, 27, 23]} df = pd.DataFrame(data) df_sorted = df.sort_values(by=['score', 'age'], ascending=[False, True]) print(df_sorted)
Output result:
name score age 2 Tom 95 27 0 Alice 90 25 3 Jerry 85 23 1 Bob 80 30
In the above code , we passed in a list containing two elements as the by
parameter, corresponding to the two sorting conditions. At the same time, we can set the sort order of each sorting condition by passing in a list of Boolean values.
3. Sort by index
In addition to sorting by columns, we can also sort by index. The sort_index()
function in Pandas can implement index sorting. Here is an example:
import pandas as pd data = {'name': ['Alice', 'Bob', 'Tom', 'Jerry'], 'score': [90, 80, 95, 85], 'age': [25, 30, 27, 23]} df = pd.DataFrame(data) df_sorted = df.sort_index(ascending=False) print(df_sorted)
Output result:
name score age 3 Jerry 85 23 2 Tom 95 27 1 Bob 80 30 0 Alice 90 25
In the above code, we sort the index by calling the sort_index()
function. The parameter ascending=False
indicates descending sorting. If you want to sort in ascending order, set it to ascending=True
.
4. Custom sorting function
Sometimes, we need to sort according to a custom function. The sort_values()
function in Pandas provides the parameter key
, which can be passed in a function for sorting. The following is an example:
import pandas as pd data = {'name': ['Alice', 'Bob', 'Tom', 'Jerry'], 'score': [90, 80, 95, 85], 'age': [25, 30, 27, 23]} df = pd.DataFrame(data) # 自定义排序函数,按照年龄和成绩之和进行排序 def custom_sort(row): return row['age'] + row['score'] df_sorted = df.sort_values(by='', key=custom_sort, ascending=False) print(df_sorted)
Output result:
name score age 2 Tom 95 27 3 Jerry 85 23 0 Alice 90 25 1 Bob 80 30
In the above code, we customized a sorting function custom_sort()
and passed it insort_values()
In the key
parameter of the function. This function compares sizes based on the sum of the "age" and "score" columns of the input rows.
Summary:
This article introduces several aspects of Pandas sorting techniques: sorting by single column, sorting by multiple columns, sorting by index, and custom sorting functions. The flexible use of these sorting functions makes it easy to sort data according to specific conditions. I hope the sample code in this article will be helpful to everyone in practice.
The above is the detailed content of Revealed: Detailed explanation of pandas techniques for sorting by specific conditions. For more information, please follow other related articles on the PHP Chinese website!