pandas dataframe group year index by decade

后端 未结 4 1213
感动是毒
感动是毒 2021-02-06 03:27

suppose I have a dataframe with index as monthy timestep, I know I can use dataframe.groupby(lambda x:x.year) to group monthly data into yearly and apply other oper

相关标签:
4条回答
  • 2021-02-06 04:02

    Use the year attribute of index:

    df.groupby(df.index.year)
    
    0 讨论(0)
  • 2021-02-06 04:04

    if your Data Frame has Headers say : DataFrame ['Population','Salary','vehicle count']

    Make your index as Year: DataFrame=DataFrame.set_index('Year')

    use below code to resample data in decade of 10 years and also gives you some of all other columns within that dacade

    datafame=dataframe.resample('10AS').sum()

    0 讨论(0)
  • 2021-02-06 04:16

    To get the decade, you can integer-divide the year by 10 and then multiply by 10. For example, if you're starting from

    >>> dates = pd.date_range('1/1/2001', periods=500, freq="M")
    >>> df = pd.DataFrame({"A": 5*np.arange(len(dates))+2}, index=dates)
    >>> df.head()
                 A
    2001-01-31   2
    2001-02-28   7
    2001-03-31  12
    2001-04-30  17
    2001-05-31  22
    

    You can group by year, as usual (here we have a DatetimeIndex so it's really easy):

    >>> df.groupby(df.index.year).sum().head()
             A
    2001   354
    2002  1074
    2003  1794
    2004  2514
    2005  3234
    

    or you could do the (x//10)*10 trick:

    >>> df.groupby((df.index.year//10)*10).sum()
               A
    2000   29106
    2010  100740
    2020  172740
    2030  244740
    2040   77424
    

    If you don't have something on which you can use .year, you could still do lambda x: (x.year//10)*10).

    0 讨论(0)
  • 2021-02-06 04:28

    lets say your date column goes by the name Date, then you can group up

    dataframe.set_index('Date').ix[:,0].resample('10AS', how='count')

    Note: the ix - here chooses the first column in your dataframe

    You get the various offsets: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases

    0 讨论(0)
提交回复
热议问题