【问题标题】:Pandas series sort by month indexPandas 系列按月份索引排序
【发布时间】:2017-04-10 12:19:02
【问题描述】:

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

我正在尝试按月对索引列为月的上述系列进行排序。然而,排序函数不是按月份的日历顺序排序,而是按月份名称的字典顺序排序。如何正确排序上述内容?猜猜我必须指定索引类型是月份而不是字符串。任何帮助表示赞赏。代码如下:sn-p。

import calendar
movies = release_dates[release_dates.title.str.contains('Christmas') & (release_dates.country=='USA')]
movies = movies.date.dt.month.apply(lambda x: calendar.month_abbr[x])
counts = movies.value_counts()
counts

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    您可以将已排序的CategoricalIndexsort_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
    

    或使用DataFrame.reindex - 但如果缺少某些值,请添加 NaN 行:

    df = df.reindex(cats)
    

    【讨论】:

    • c:\python 3.5\lib\site-packages\pandas\indexes\category.py:128:RuntimeWarning:值和类别具有不同的 dtype。您的意思是使用“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: 在值中找不到任何类别。您的意思是使用“Categorical.from_codes(codes, categories)”吗?数据=分类(数据,类别=类别,有序=有序
    • 我觉得你可以试试。
    • 你的熊猫版本是什么?
    【解决方案2】:

    补充@jezrael 的非常有帮助的答案:

    在 pandas 0.25.1 中,sorted 已被 ordered 替换为 pandas.CategoricalIndex

    老办法:

    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()
    

    错误

    ---------------------------------------------------------------------------
    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'
    

    新方式:

    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()
    

    【讨论】:

      【解决方案3】:

      好吧,这不是很复杂。我确信分类会起作用,只是我无法使用分类解决问题。 我所做的是-

      1. 当月份表示为整数时按月份排序
      2. 对生成的系列应用了一个映射器在索引上将整数月份转换为缩写字符串

      我确信有更有效的方法可以解决这个问题,所以如果您有更好的方法,请发布相同的方法。

          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()

      【讨论】:

        猜你喜欢
        • 2018-06-11
        • 2021-01-07
        • 2016-01-16
        • 2020-04-30
        • 2018-08-29
        • 2016-02-15
        • 2015-04-06
        • 2018-10-28
        相关资源
        最近更新 更多