如何根据列名或行索引对 Pandas DataFrame 进行排序?
Pandas dataframe.sort_index()
方法沿给定轴按标签对对象进行排序。基本上,排序算法应用于轴标签而不是数据框中的实际数据,并基于数据重新排列。我们可以自由选择我们想要应用的排序算法。我们可以使用三种可能的排序算法“快速排序”、“合并排序”和“堆排序”。
Syntax: DataFrame.sort_index(axis=0, level=None, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’, sort_remaining=True, by=None)
Parameters :
axis : p for index, 1 for columns to direct sorting
level : if not None, sort on values in specified index level(s)
ascending : Sort ascending vs. descending
inplace : if True, perform operation in-place i.e changes in actual dataframe otherwise return a sorted dataframe.
kind : {‘quicksort’, ‘mergesort’, ‘heapsort’}, default ‘quicksort’. Choice of sorting algorithm. See also ndarray.np.sort for more information. mergesort is the only stable algorithm. For DataFrames, this option is only applied when sorting on a single column or label.
na_position : [{‘first’, ‘last’}, default ‘last’] First puts NaNs at the beginning, last puts NaNs at the end. Not implemented for MultiIndex.
sort_remaining : If true and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level
Return : sorted_obj : DataFrame
使用列表字典创建一个简单的数据框,例如列名是:'Name'、'Age'、'Place'和'College'。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f', 'g',
'i', 'j', 'k', 'd'])
# show the dataframe
details
输出:
示例 1:根据行索引标签名称对数据框的行进行排序。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f',
'g', 'i', 'j', 'k', 'd'])
# sort the rows of dataframe
# based on row index
rslt_df = details.sort_index()
# show the resultant Dataframe
rslt_df
输出:
示例 2:根据行索引标签按降序对数据框的行进行排序。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f',
'g', 'i', 'j', 'k', 'd'])
# sort the rows of dataframe in descending
# order based on row index
rslt_df = details.sort_index(ascending = False)
# show the resultant Dataframe
rslt_df
输出:
示例 3:基于行索引标签对数据框的行进行排序。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f',
'g', 'i', 'j', 'k', 'd'])
# sort the rows of dataframe in Place
# based on row index
details.sort_index(inplace = True)
# show the resultant Dataframe
details
输出:
示例 4:根据列名对数据框的列进行排序。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f',
'g', 'i', 'j', 'k', 'd'])
# sort a dataframe based on column names
rslt_df = details.sort_index(axis = 1)
# show the resultant Dataframe
rslt_df
输出:
示例 5:根据列名按降序对数据框进行排序。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f',
'g', 'i', 'j', 'k', 'd'])
# sort a dataframe in descending
# order based on column names
rslt_df = details.sort_index(ascending = False, axis = 1)
# show the resultant Dataframe
rslt_df
输出:
示例 6:根据列名对数据框进行就地排序。
# import pandas library as pd
import pandas as pd
# List of Tuples
students = [('Ankit', 22, 'Up', 'Geu'),
('Ankita', 31, 'Delhi', 'Gehu'),
('Rahul', 16, 'Tokyo', 'Abes'),
('Simran', 41, 'Delhi', 'Gehu'),
('Shaurya', 33, 'Delhi', 'Geu'),
('Harshita', 35, 'Mumbai', 'Bhu' ),
('Swapnil', 35, 'Mp', 'Geu'),
('Priya', 35, 'Uk', 'Geu'),
('Jeet', 35, 'Guj', 'Gehu'),
('Ananya', 35, 'Up', 'Bhu')
]
# Create a DataFrame object from
# list of tuples with columns
# and indices.
details = pd.DataFrame(students, columns =['Name', 'Age',
'Place', 'College'],
index =[ 'b', 'c', 'a', 'e', 'f',
'g', 'i', 'j', 'k', 'd'])
# sort a dataframe in place
# based on column names
details.sort_index(inplace = True, axis = 1)
# show the resultant Dataframe
details
输出: