Pandas Dataframe.sort_values() Last Updated : 29 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In Pandas, sort_values() function sorts a DataFrame by one or more columns in ascending or descending order. This method is essential for organizing and analyzing large datasets effectively.Syntax: DataFrame.sort_values(by, axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last')Parameter: by: Column name(s) to sort by (single or list).axis: 0 or 'index' for sorting rows; 1 or 'columns' for sorting columns.ascending: Boolean; True for ascending, False for descending.inplace: Boolean; if True, modifies the original DataFrame.kind: Sorting algorithm: 'quicksort', 'mergesort', or 'heapsort'.na_position: 'first' or 'last'; defines position of NaN values. Default is 'last'.The output returns a sorted DataFrame with the same dimensions as the original.Sorting DataFrame by Column This example demonstrates how to sort a DataFrame by player names in ascending order.Dataset Link: nba.csv Python import pandas as pd # Load DataFrame from CSV file data = pd.read_csv("nba.csv") print("NBA Dataset:") display(data.head()) # Sort the DataFrame by the 'Name' column in ascending order data.sort_values("Name", axis=0, ascending=True, inplace=True, na_position='last') # Display the sorted DataFrame print("\nSorted DataFrame:") display(data.head()) OutputPandas DataFrame sorted by 'Name' ColumnIn the sorted DataFrame, the rows are arranged alphabetically by player names. The index may appear jumbled because sorting affects the order of rows.Sorting DataFrame with Custom NaN Value PlacementIn this example, the DataFrame is sorted by the "Salary" column, with null values positioned at the top (instead of the default last). Python import pandas as pd # Load DataFrame from CSV file data = pd.read_csv("nba.csv") # Sort by 'Salary' column, placing NaN values at the top data.sort_values("Salary", axis=0, ascending=True, inplace=True, na_position='first') # Display the sorted DataFrame print("DataFrame sorted by 'Salary' column by placing NaN values at the top:") display(data) Output Here, NaN values in the 'Salary' column are placed at the top, followed by the sorted salary values.Additionally, you can adjust the following parameters: The inplace parameter allows for modifying the original DataFrame directly, avoiding the need to assign the result to a new variable.The kindparameter gives flexibility in choosing the sorting algorithm based on performance or requirements, such as 'quicksort', 'mergesort', or 'heapsort'.Sorting can be done by multiple columns by passing a list to the by parameter, enabling more complex sorting.The sort_values() function in Pandas is a versatile tool for sorting DataFrames by specific columns, with multiple options for handling null values and choosing sorting algorithms. Comment More info K Kartikaybhutani Follow Improve Article Tags : Pandas python-modules Python-pandas Python pandas-dataFrame Pandas-DataFrame-Methods +1 More Explore Pandas Tutorial 6 min read IntroductionPandas Introduction 3 min read How to Install Pandas in Python? 5 min read How To Use Jupyter Notebook - An Ultimate Guide 5 min read Creating ObjectsCreating a Pandas DataFrame 2 min read Python Pandas Series 5 min read Creating a Pandas Series 3 min read Viewing DataPandas Dataframe/Series.head() method - Python 3 min read Pandas Dataframe/Series.tail() method - Python 3 min read Pandas DataFrame describe() Method 4 min read Selection & SlicingDealing with Rows and Columns in Pandas DataFrame 5 min read Pandas Extracting rows using .loc[] - Python 3 min read Extracting rows using Pandas .iloc[] in Python 7 min read Indexing and Selecting Data with Pandas 4 min read Boolean Indexing in Pandas 6 min read Python | Pandas DataFrame.ix[ ] 2 min read Python | Pandas Series.str.slice() 3 min read How to take column-slices of DataFrame in Pandas? 2 min read OperationsPython | Pandas.apply() 4 min read Apply function to every row in a Pandas DataFrame 3 min read Python | Pandas Series.apply() 3 min read Pandas dataframe.aggregate() | Python 2 min read Pandas DataFrame mean() Method 2 min read Python | Pandas Series.mean() 2 min read Python | Pandas dataframe.mad() 2 min read Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series 2 min read Python | Pandas dataframe.sem() 3 min read Python | Pandas Series.value_counts() 2 min read Pandas Index.value_counts()-Python 3 min read Applying Lambda functions to Pandas Dataframe 6 min read Manipulating DataAdding New Column to Existing DataFrame in Pandas 6 min read Python | Delete rows/columns from DataFrame using Pandas.drop() 4 min read Python | Pandas DataFrame.truncate 3 min read Python | Pandas Series.truncate() 2 min read Iterating over rows and columns in Pandas DataFrame 4 min read Pandas Dataframe.sort_values() 2 min read Python | Pandas Dataframe.sort_values() | Set-2 3 min read How to add one row in existing Pandas DataFrame? 4 min read Grouping DataPandas GroupBy 4 min read Grouping Rows in pandas 2 min read Combining Multiple Columns in Pandas groupby with Dictionary 2 min read Merging, Joining, Concatenating and ComparingPython | Pandas Merging, Joining and Concatenating 8 min read Python | Pandas Series.str.cat() to concatenate string 3 min read Python - Pandas dataframe.append() 4 min read Python | Pandas Series.append() 4 min read Pandas Index.append() - Python 2 min read Python | Pandas Series.combine() 3 min read Add a row at top in pandas DataFrame 1 min read Python | Pandas str.join() to join string/list elements with passed delimiter 2 min read Join two text columns into a single column in Pandas 2 min read How To Compare Two Dataframes with Pandas compare? 5 min read How to compare the elements of the two Pandas Series? 3 min read Working with Date and TimePython | Working with date and time using Pandas 8 min read Python | Pandas Timestamp.timestamp 3 min read Python | Pandas Timestamp.now 3 min read Python | Pandas Timestamp.isoformat 2 min read Python | Pandas Timestamp.date 2 min read Python | Pandas Timestamp.replace 3 min read Pandas.to_datetime()-Python 3 min read Python | pandas.date_range() method 4 min read Like