search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | sort_values method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.sort_values(~) method sorts the source DataFrame either by column or row values.

Parameters

1. bylink | string or list<string>

The name of the column(s) or row to sort.

2. axislink | string or int | optional

Whether to sort by row or column:

Axis

Description

0 or "index"

DataFrame will be sorted by column values.

1 or "columns"

DataFrame will be sorted by row values.

By default, axis=0.

3. ascendinglink | boolean or list<booleans> | optional

Whether to sort in ascending or descending order. By default, ascending=True.

4. inplace | boolean | optional

  • If True, then the source DataFrame will be directly modified, and no new DataFrame will be created.

  • If False, then a new DataFrame will be created, and the source DataFrame will be kept intact.

By default, inplace=False.

5. kind | string | optional

The sorting algorithm to use:

Kind

Speed

Worst case

Memory

Stable

quicksort

1 (fast)

O(n^2)

0

no

mergesort

2

O(nlogn)

~n/2

yes

heapsort

3 (slow)

O(nlogn)

0

no

By default, kind="quicksort".

NOTE

Sorting algorithms that are "stable" retain the relative ordering of duplicate values. For instance, suppose you are sorting the array [(2,3), (2,1), (4,5)] by the first element of each tuple. We have a duplicate value of 2 here, and stable sorting algorithms ensure that (2,3) will always come before (2,1) since that is how they are ordered originally. Unstable searches provide no guarantee that such ordering is retained.

6. na_positionlink | string | optional

Where to place NaN values:

Value

Description

"first"

Place NaNs at the beginning

"last"

Place NaNs at the end

By default, na_position="last".

7. ignore_indexlink | boolean | optional

  • If True, then the index of the sorted DataFrame will be 0,1,...,n-1, where n is the number of rows of the DataFrame.

  • If False, then the index names will be kept as is.

By default, ignore_index=False.

Return Value

A DataFrame sorted by row or column values.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"A":[5,3,1,3],"B":["c","d","a","a"]})
df
   A  B
0  5  c
1  3  d
2  1  a
3  3  a

Sorting by column value

To sort by column A:

df.sort_values("A")
   A  B
2  1  a
1  3  d
3  3  a
0  5  c

Sorting by multiple columns

To sort by multiple columns, pass in a list of column labels:

df.sort_values(["A","B"])
   A  B
2  1  a
3  3  a
1  3  d
0  5  c

Here, the rows are sorted first by the values in column A. When there are duplicate values, we sort by the values in column B. This is the reason why the value "a" is guaranteed to come before "d" in this case.

Sorting by row value

Consider the following DataFrame:

df = pd.DataFrame({"A":[5,9,4],"B":[1,3,2]}, index=["a","b","c"])
df
   A  B
a  5  1 
b  9  3
c  4  2

To sort by values in row "b":

df.sort_values("b", axis=1)
   B  A
a  1  5
b  3  9
c  2  4

Sorting in descending order

Consider the following DataFrame:

df = pd.DataFrame({"A":[5,9,5],"B":[4,3,1]}, index=["a","b","c"])
df
   A  B
a  5  4
b  9  3
c  5  1

By default, values are sorted in ascending order. To sort in descending order instead, set ascending=False:

df.sort_values("A", ascending=False)
   A  B
b  9  3
a  5  4
c  5  1

Passing an array of booleans

If you're sorting by multiple columns, you can pass a list of booleans to specify how each column should be sorted.

df.sort_values(["A","B"], ascending=[False, True])
   A  B
b  9  3
c  5  1
a  5  4

Here, we are sorting by column A values in descending order, while sorting by column B in ascending order.

Specifying na_position

Consider the following DataFrame:

df = pd.DataFrame({"A":[pd.np.NaN,5,4]}, index=["a","b","c"])
df
   A
a  NaN
b  5.0
c  4.0

By default, na_position="last", which means that NaN are placed at the end:

df.sort_values(by="A")   # na_position="last"
   A
c  4.0
b  5.0
a  NaN

To place NaN in the beginning instead:

df.sort_values(by="A", na_position="first")
   A
a  NaN
c  4.0
b  5.0

Specifying ignore_index

Consider the following DataFrame:

df = pd.DataFrame({"A":[5,9,2]}, index=["a","b","c"])
df
   A
a  5
b  9
c  2

By default, the index names are kept:

df.sort_values(by="A")
   A
c  2
a  5
b  9

By setting ignore_index=True, we can reset the index:

df.sort_values(by="A", ignore_index=True)
   A
0  2
1  5
2  9
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...