2021年5月4日星期二

How to sort the order of index for value_counts() to a custom order?

After performing value_counts() to a series in a dataframe,

>>> n_fires_by_month = df['month'].value_counts()  >>> n_fires_by_month  

I get this:

| -------| -------------- |  | aug    | 184            |  | sep    | 172            |  | mar    | 54             |  | jul    | 32             |  | feb    | 20             |  | jun    | 17             |  | oct    | 15             |  | dec    | 9              |  | apr    | 9              |  | may    | 2              |  | jan    | 2              |  | nov    | 1              |  

After running value_counts(), I want to reset the index of the resulting table in order of month so I want the resulting table to look like this:

| -------| -------------- |  | jan    | 2              |  | feb    | 20             |  | mar    | 54             |  | apr    | 9              |  | may    | 2              |  | jun    | 17             |  | jul    | 32             |  | aug    | 184            |  | sep    | 172            |  | oct    | 15             |  | nov    | 1              |  | dec    | 9              |  

Please help.

https://stackoverflow.com/questions/67394115/how-to-sort-the-order-of-index-for-value-counts-to-a-custom-order May 05, 2021 at 09:35AM

没有评论:

发表评论