pandas data sorting .sort_index () and .sort_values ()

Original link: https://www.jianshu.com/p/f0ed06cd5003

 

PD PANDAS AS Import
DF = pd.DataFrame (......)
Description: The following "df" is DataFrame object.

1. df. sort_values()

Action: either based on column data, the data can also be sorted according to a row.
Note: must be specified by the parameters that you must specify which lines or which columns; can not sort columns based on index name (() is executed by .sort_index) and name

Called

DataFrame.sort_values (by, Axis = 0, Ascending = True, False InPlace =, = kind 'quicksort', na_position = 'Last')
Axis: {0 or 'index',. 1 or 'Columns'}, 0 default, default according to the sort column, i.e. the longitudinal sorting; if 1, it is a transverse sort.
by: str or list of str; if axis = 0, then by = "column name"; if the axis = 1, then by = "row names."
ascending: Boolean, True then ascending order, if by = [ 'column name 1', '2 Column Name'], the parameter may be [True, False], i.e. the first field in ascending, descending second.
inplace: Boolean, whether to replace existing data with the frame data block sorted.
kind: sorting method, { 'quicksort', 'mergesort ', 'heapsort'}, default 'quicksort'. It does not seem to be too concerned.
na_position: { 'first', ' last'}, default 'last', the default value is missing at the bottom surface.

Example:

raw data

df = pd.DataFrame({'b':[1,2,3,2],'a':[4,3,2,1],'c':[1,3,8,2]},index=[2,0,1,3]) 
    b   a   c
2   1   4   1
0   2   3   3
1   3   2   8
3   2   1   2

1. Sort Ascending column b

df.sort_values (by = ' B ' ) # is equivalent to df.sort_values (by = 'B', Axis = 0) 
    BAC
 2. 1. 1. 4 
0    2. 3. 3 
. 3 2. 1 2 
. 1 2. 8. 3

2. The first press descending column b, then a column in ascending order

df.sort_values(by=['b','a'],axis=0,ascending=[False,True]) 
    b   a   c
1   3   2   8
3   2   1   2
0   2   3   3
2   1   4   1

3. arranged in ascending row 3

df.sort_values (by =. 3, Axis =. 1) # must be specified. 1 = Axis 
    ABC
 2. 4. 1. 1 
0    . 3 2. 3 
. 1. 3 2. 8 
. 3. 1 2 2

4. ascending row 3, row 0 down arrangement

df.sort_values(by=[3,0],axis=1,ascending=[True,False])
    a   c   b
2   4   1   1
0   3   3   2
1   2   8   3
3   1   2   2

Note: multi-column (multi-line) when sorting, press columns (rows) of the sort top surface, if the same internal data, then (line) inside of the same sort data with the next column, and so on. How internal duplicated data, then the following arrangement is not performed. I.e., the top surface of the first meet the sorting parameters, then the back row of the parameter

2. df. sort_index()

Role: The default row label according to sort all rows or columns based on column labels of all sort, or specified a column or a row of columns to sort.
Note:.. Df sort_index () can be completed and df sort_values () function exactly the same, but more is recommended only python df sort_index () on "according to the row labels" and "according to the column label" sort, with the other sort. df.sort_values ().

Called

sort_index (Axis = 0, Level = None, Ascending = True, InPlace = False, kind = 'quicksort', na_position = 'Last', sort_remaining = True, by = None)
Axis: 0 sorted row name; product 1 according to the column name Sort
level: default None, or arranged in a given order --- seemingly is not level, the document
ascending: ascending default True; False descending
inplace: default False, otherwise the data after sorting the data frame directly replace the original
kind: sorting method, { 'quicksort', 'mergesort ', 'heapsort'}, default 'quicksort'. It does not seem to be too concerned.
na_position: missing values by default at the last { "First", "Last"}
by: according to one or more columns to sort data, but not recommended by the parameter seemingly

Example:

source data

import pandas as pd  
df = pd.DataFrame({'b':[1,2,2,3],'a':[4,3,2,1],'c':[1,3,8,2]},index=[2,0,1,3]) 
    b   a   c
2   1   4   1
0   2   3   3
1   3   2   8
3   2   1   2

1. Default ascending order "Row Labels" (Recommended)

df.sort_index () # Default Sort "Row Labels" in ascending order, or df.sort_index (Axis = 0, = True Ascending) 
    BAC 
0    2. 3. 3 
. 1 2. 8. 3 
2. 1. 1. 4 
. 3. 1 2 2

2. ascending order "column label" (Recommended)

df.sort_index (Axis =. 1) # Press "column label" ascending order 
    ABC
 2. 4. 1. 1 
0    . 3 2. 3 
. 1. 3 2. 8 
. 3. 1 2 2

 

 

Guess you like

Origin www.cnblogs.com/loubin/p/11297681.html