pandas 按正确顺序对月份名称的熊猫数据框进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27179082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sort pandas dataframe of month names in correct order
提问by yoshiserry
I have a dataframe with names of months of the year, I.e. Jan, Feb, March etc
我有一个包含一年中月份名称的数据框,即一月、二月、三月等
and I want to sort the data first by month, then by category so it looks like
我想先按月对数据进行排序,然后按类别排序,看起来像
Month_Name | Cat
Jan 1
Jan 2
Jan 3
Feb 1
Feb 2
Feb 3
回答by Katriel
pandasdoesn't do custom sort functions for you, but you can easily add a temporary column which is the index of the month, and then sort by that
pandas不会为您执行自定义排序功能,但您可以轻松添加一个临时列,该列是月份的索引,然后按该列排序
months = {datetime.datetime(2000,i,1).strftime("%b"): i for i in range(1, 13)}
df["month_number"] = df["month_name"].map(months)
df.sort(columns=[...])
You may wish to take advantage of pandas' good date parsing when reading in your dataframe, though: if you store the dates as dates instead of string month names then you'll be able to sort natively by them.
不过,您可能希望pandas在读取数据帧时利用' 良好的日期解析:如果您将日期存储为日期而不是字符串月份名称,那么您将能够通过它们进行本地排序。
回答by Dinesh Babu
Use Sort_Dataframeby_MonthandNumeric_colsfunction to sort dataframe by month and numeric column:
使用Sort_Dataframeby_MonthandNumeric_cols函数按月份和数字列对数据框进行排序:
You need to install two packages are shown below.
您需要安装两个包,如下所示。
pip install sorted-months-weekdays
pip install sort-dataframeby-monthorweek
Example:
例子:
import pandas as pd
from sorted_months_weekdays import *
from sort_dataframeby_monthorweek import *
df = pd.DataFrame([['Jan',23],['Jan',16],['Dec',35],['Apr',79],['Mar',53],['Mar',12],['Feb',3]], columns=['Month','Sum'])
df
Out[11]:
Month Sum
0 Jan 23
1 Jan 16
2 Dec 35
3 Apr 79
4 Mar 53
5 Mar 12
6 Feb 3
To get sorted dataframe by month and numeric column I have used above function.
为了按月和数字列对数据框进行排序,我使用了上面的函数。
Sort_Dataframeby_MonthandNumeric_cols(df = df, monthcolumn='Month',numericcolumn='Sum')
Out[12]:
Month Sum
0 Jan 16
1 Jan 23
2 Feb 3
3 Mar 12
4 Mar 53
5 Apr 79
6 Dec 35

