## Pandas series sort by month index

pandas series sort by index

pandas sort index by list

pandas series sort by value and index

pandas reorder index

pandas sort by month

pandas sort by index and column

pandas sort by multiple columns

Dec 47 Nov 36 Oct 14 Sep 2 Jan 2 Aug 2 May 1 Apr 1 Jun 1 Jul 1 Feb 1 Name: date, dtype: int64

You can use sorted `CategoricalIndex`

with `sort_index`

:

cats = ['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec'] df.index = pd.CategoricalIndex(df.index, categories=cats, ordered=True) df = df.sort_index() print (df) date Jan 2 Feb 1 Apr 1 May 1 Jun 1 Jul 1 Aug 2 Sep 2 Oct 14 Nov 36 Dec 47

Or use `DataFrame.reindex`

- but if some value is missing add NaNs rows:

df = df.reindex(cats)

**Sort a pandas's dataframe series by month name?,** Be aware to use the same key to sort and groupby in the df on making categorical index ordered in Pandas series sort by month index. pandas.Series.sort_index¶ Series.sort_index (self, axis = 0, level = None, ascending = True, inplace = False, kind = 'quicksort', na_position = 'last', sort_remaining = True, ignore_index: bool = False) [source] ¶ Sort Series by index labels. Returns a new Series sorted by label if inplace argument is False, otherwise updates the original

Okay it was not very complex. I'm sure Categorical would have worked just that I was unable to solve the problem using Categorical. What I did was-

- Sort by month while months were being represented as integers
- To the resulting series applied a mapper on the index to convert the integer month into an abbreviated string

I'm sure there are more efficient ways of solving this, so if you have a better way please post the same.

import calendar months = release_dates[release_dates.title.str.contains('Christmas') & (release_dates.country=='USA')].date.dt.month counts = months.value_counts() counts.sort_index(inplace=True) counts.index = map(lambda x: calendar.month_abbr[x], counts.index) counts.plot.bar()

**pandas.Series.sort_values — pandas 1.1.0 documentation,** Sort a Series in ascending or descending order by some criterion. Parameters. axis{0 or 'index'}, default 0. Axis to direct sorting. The value 'index� Series is a one-dimensional labeled array capable of holding data of the type integer, string, float, python objects, etc. The axis labels are collectively called index. Now, Let’s see a program to sort a Pandas Series. For sorting a pandas series the Series.sort_values() method is used

*Adding to the very helpful answer by @jezrael:*

In pandas 0.25.1 `sorted`

has been replaced by `ordered`

per pandas.CategoricalIndex

**Old way:**

df.index = pd.CategoricalIndex(df.index, categories=['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec'], sorted=True) df = df.sort_index()

**Error**

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-468-3f0ab66734d4> in <module> 2 net.index = pd.CategoricalIndex(net.index, 3 categories=['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec'], ----> 4 sorted=True) 5 net = net.sort_index() 6 net TypeError: __new__() got an unexpected keyword argument 'sorted'

**New way:**

df.index = pd.CategoricalIndex(df.index, categories=['Jan', 'Feb', 'Mar', 'Apr','May','Jun', 'Jul', 'Aug','Sep', 'Oct', 'Nov', 'Dec'], ordered=True) df = df.sort_index()

**pandas.Series.sort_index — pandas 1.1.0 documentation,** Sort Series by index labels. Returns a new Series sorted by label if inplace argument is False , otherwise updates the original series and returns None. A time series is a sequence of numerical data points in successive order i.e. time periods or intervals. Pandas has many tools specifically built for working with the time stamped data. In this

**pandas.Index.sort_values — pandas 1.1.0 documentation,** The indices that the index itself was sorted by. See also. Series.sort_values. Sort values of a Series. DataFrame. Dataframes have a sort_index method which returns a copy by default. Pass inplace=True to operate in place. import pandas as pd df = pd.DataFrame([1, 2, 3, 4, 5], index= [100, 29, 234, 1, 150], columns= ['A']) df.sort_index(inplace=True) print(df.to_string())

**pandas.DataFrame.sort_index — pandas 1.1.0 documentation,** Not implemented for MultiIndex. sort_remainingbool, default True. If True and sorting by level and index is multilevel, sort by other levels too (in order)� Sort object by labels (along an axis). Parameters axis {0 or ‘index’, 1 or ‘columns’}, default 0. The axis along which to sort. The value 0 identifies the rows, and 1 identifies the columns. level int or level name or list of ints or list of level names. If not None, sort on values in specified index level(s). ascending bool, default

**Python,** sort_remaining : If true and sorting by level and index is multilevel, sort by other levels too (in order) after sorting by specified level. Returns : Series. Example #1: � pandas.Series.sort_values¶ Series.sort_values (self, axis = 0, ascending = True, inplace = False, kind = 'quicksort', na_position = 'last', ignore_index = False) [source] ¶ Sort by the values. Sort a Series in ascending or descending order by some criterion. Parameters axis {0 or ‘index’}, default 0. Axis to direct sorting.

##### Comments

- c:\python 3.5\lib\site-packages\pandas\indexes\category.py:128: RuntimeWarning: Values and categories have different dtypes. Did you mean to use 'Categorical.from_codes(codes, categories)'? data = Categorical(data, categories=categories, ordered=ordered) c:\python 3.5\lib\site-packages\pandas\indexes\category.py:128: RuntimeWarning: None of the categories were found in values. Did you mean to use 'Categorical.from_codes(codes, categories)'? data = Categorical(data, categories=categories, ordered=ordered
- I think you can try it.
- What is your pandas version?