YouTip LogoYouTip

Pandas Df Sort Values

[![Image 1: Pandas Common Functions](#) Pandas Common Functions](#) * * * `df.sort_values()` is a function in Pandas used to sort DataFrame data by one or more columns. Sorting is a fundamental operation in data analysis that helps you better understand data and discover patterns. `sort_values()` supports sorting by single or multiple columns, ascending or descending order, and can handle missing values, making it an indispensable tool in data processing. * * * ## Basic Syntax and Parameters `sort_values()` is a member function of DataFrame, called through the dot operator `.`. ### Syntax Format DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, key=None) ### Parameter Description | Parameter | Type | Required | Description | Default Value | | --- | --- | --- | --- | --- | | by | str or str list | Required | Specifies the column name(s) to sort by. If it's a list, sorts sequentially in the order of the list. | None | | axis | int or str | Optional | Specifies the axis to sort. `0` or `'index'` means sort by columns (default); `1` or `'columns'` means sort by rows. | 0 | | ascending | bool or bool list | Optional | Specifies the sort order. `True` means ascending (small to large); `False` means descending (large to small). If it's a list, it corresponds one-to-one with columns in `by`. | True | | inplace | bool | Optional | If `True`, modifies the original DataFrame directly without returning a new object; if `False`, returns a new DataFrame, leaving the original data unchanged. | False | | kind | str | Optional | Specifies the sorting algorithm. `'quicksort'` (quick sort), `'mergesort'` (merge sort), `'heapsort'` (heap sort), `'stable'` (stable sort). | 'quicksort' | | na_position | str | Optional | Specifies the position of missing values (NaN). `'first'` means place at the beginning; `'last'` means place at the end. | 'last' | | ignore_index | bool | Optional | If `True`, resets the index after sorting, numbering from 0; if `False`, keeps the original index. | False | | key | callable | Optional | Applies a function to the data before sorting, commonly used for custom sorting rules. | None | ### Return Value Description * Returns a new DataFrame (if `inplace=False`), or `None` (if `inplace=True`). * The data in the returned DataFrame has been sorted according to the specified rules. * * * ## Examples Let's thoroughly master the usage of `sort_values()` through a series of examples. ### Example 1: Sort by Single Column in Ascending Order The simplest usage is to sort by the values of a certain column in ascending order. ## Example import pandas as pd # Create a student scores DataFrame data ={ 'Name': ['Zhang San','Li Si','Wang Wu','Zhao Liu','Qi Qian'], 'Math': [85,92,78,90,88], 'English': [90,85,92,88,78] } df = pd.DataFrame(data) print("Original data:") print(df) print("=" * 50) # Sort by math score in ascending order df_sorted = df.sort_values(by='Math') print("After sorting by math score in ascending order:") print(df_sorted) **Expected Output:** Original data: Name Math English0 Zhang San 85 901 Li Si 92 852 Wang Wu 78 923 Zhao Liu 90 884 Qi Qian 88 78==================================================After sorting by math score in ascending order: Name Math English2 Wang Wu 78 920 Zhang San 85 904 Qi Qian 88 783 Zhao Liu 90 881 Li Si 92 85 **Code Analysis:** 1. The default sort order is ascending (small to large). 2. Math score 78 is at the top, 92 is at the end. 3. The index remains unchanged after sorting (2, 0, 4, 3, 1). ### Example 2: Sort by Single Column in Descending Order Using `ascending=False`Can sort in descending order. ## Example import pandas as pd # Create a student scores DataFrame data ={ 'Name': ['Zhang San','Li Si','Wang Wu','Zhao Liu','Qi Qian'], 'Math': [85,92,78,90,88], 'English': [90,85,92,88,78] } df = pd.DataFrame(data) print("Original data:") print(df) print("=" * 50) # Sort by math score in descending order df_sorted = df.sort_values(by='Math', ascending=False) print("After sorting by math score in descending order:") print(df_sorted) **Expected Output:** Original data: Name Math English0 Zhang San 85 901 Li Si 92 852 Wang Wu 78 923 Zhao Liu 90 884 Qi Qian 88 78==================================================After sorting by math score in descending order: Name Math English1 Li Si 92 853 Zhao Liu 90 884 Qi Qian 88 780 Zhang San 85 902 Wang Wu 78 92 **Code Analysis:** * Using `ascending=False`Can sort in descending order from largest to smallest. * Li Si's math score of 92 is at the top. ### Example 3: Sort by Multiple Columns You can sort by multiple columns, sorting by the first column first, and when the first column is the same, sort by the second column. ## Example import pandas as pd # Create a student scores DataFrame data ={ 'Name': ['Zhang San','Li Si','Wang Wu','Zhao Liu','Qi Qian'], 'Class': ['Class 1','Class 2','Class 1','Class 2','Class 1'], 'Math': [85,92,78,90,88] } df = pd.DataFrame(data) print("Original data:") print(df) print("=" * 50) # Sort by class first, then by math score (both ascending) df_sorted = df.sort_values(by=['Class','Math']) print("After sorting by class, then by math score:") print(df_sorted) print("=" * 50) # Sort by class ascending, math score descending df_sorted2 = df.sort_values(by=['Class','Math'], ascending=[True,False]) print("Class ascending, math score descending:") print(df_sorted2) **Expected Output:** Original data: Name Class Math0 Zhang San Class 1 851 Li Si Class 2 922 Wang Wu Class 1 783 Zhao Liu Class 2 904 Qi Qian Class 1 88==================================================After sorting by class, then by math score: Name Class Math2 Wang Wu Class 1 780 Zhang San Class 1 854 Qi Qian Class 1 883 Zhao Liu Class 2 901 Li Si Class 2 92==================================================Class ascending, math score descending: Name Class Math4 Qi Qian Class 1 880 Zhang San Class 1 852 Wang Wu Class 1 781 Li Si Class 2 923 Zhao Liu Class 2 90 **Code Analysis:** * Using a list `by=['Class', 'Math']` can sort by multiple columns. * First sort by "Class", Class 1 comes before Class 2. * Within the same class, sort by "Math" score. * Using `ascending=[True, False]` can specify different sort directions for different columns. ### Example 4: Sort with Missing Values Use the `na_position` parameter to control the position of missing values. ## Example import pandas as pd import numpy as np # Create a DataFrame with missing values data ={ 'Name': ['Zhang San','Li Si','Wang Wu','Zhao Liu'], 'Score': [85, np.nan,92,78] } df =
← Pandas Df HeadPandas Df Astype β†’