Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a pandas Series by the index

I have a Pandas dataframe called pd, and I extract the number of unique values in one of the columns of this dataframe using the following command:

b = df.groupby('Region').size()

b is a Pandas series object and looks like this:

In [48]: b
Out[48]: 
Region
0          8
1         25
11         1
2         41
3         23
4         15
5         35
6         24
7         27
8         50
9         55
N         10

I am trying to plot a barchart of this series, however, I would like to sort it first based on the first column (because of that 11 between 1 and 2), which will be the x axis labels. I tried passing the sort command, but it sorts the series based on the values in the second column:

b.sort()

In [48]: b
Out[54]: 
Region
11         1
0          8
N         10
4         15
3         23
6         24
1         25
7         27
5         35
2         41
8         50
9         55

Well, is there a way to sort this series based on the first column?

like image 912
marillion Avatar asked Oct 02 '13 18:10

marillion


People also ask

Can you sort a series in Pandas?

For sorting a pandas series the Series. sort_values() method is used. Examples 1: Sorting a numeric series in ascending order.

How do you sort a list by index in Python?

Method 1: Sort list of lists using sort() + lambda. The anonymous nature of Python Lambda Functions indicates that they lack a name. The Python sort() can be used to perform this variation of sort by passing a function. The list can be sorted using the sort function both ascendingly and descendingly.

Can you index a Pandas series?

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.

What is the difference between sort_values () and sort_index () method?

The two major sort functions You can check the API for sort_values and sort_index at the Pandas documentation for details on the parameters. sort_values() : You use this to sort the Pandas DataFrame by one or more columns. sort_index() : You use this to sort the Pandas DataFrame by the row index.


2 Answers

You are looking for sort_index:

In [80]: b.sort_values()
Out[80]: 
6     1
11    2
9     2
1     4
10    4
2     5
3     6
4     7
8     8
5     9
dtype: int64

In [81]: b.sort_index()
Out[81]: 
1     4
2     5
3     6
4     7
5     9
6     1
8     8
9     2
10    4
11    2
dtype: int64
like image 125
bdiamante Avatar answered Oct 30 '22 09:10

bdiamante


There is only 1 'column' of values. The first 'column' is the index. Docs are here

In [8]: s = Series([3,2,1],index=[1,3,2])

In [9]: s
Out[9]: 
1    3
3    2
2    1
dtype: int64

Sort by the index

In [10]: s.sort_index()
Out[10]: 
1    3
2    1
3    2
dtype: int64

Sort by values

In [11]: s.sort_values()
Out[11]: 
2    1
3    2
1    3
dtype: int64
like image 44
Jeff Avatar answered Oct 30 '22 10:10

Jeff