在Pandas中将每日时间序列转换为一周的行数

时间:2016-12-02 03:00:14

标签: python pandas time-series

我有一个熊猫时间序列:

days = pd.DatetimeIndex([
                    '2011-01-01T00:00:00.000000000', 
                    '2011-01-02T00:00:00.000000000',
                    '2011-01-03T00:00:00.000000000', 
                    '2011-01-04T00:00:00.000000000',
                    '2011-01-05T00:00:00.000000000', 
                    '2011-01-06T00:00:00.000000000',
                    '2011-01-07T00:00:00.000000000', 
                    '2011-01-08T00:00:00.000000000',
                    '2011-01-09T00:00:00.000000000', 
                    '2011-01-11T00:00:00.000000000', 
                    '2011-01-12T00:00:00.000000000',
                    '2011-01-13T00:00:00.000000000', 
                    '2011-01-14T00:00:00.000000000',
                    '2011-01-16T00:00:00.000000000',
                    '2011-01-18T00:00:00.000000000',
                    '2011-01-19T00:00:00.000000000',
                    '2011-01-21T00:00:00.000000000',
                   ])
counts = [85, 97, 24, 64, 3, 37, 73, 86, 87, 82, 75, 84, 43, 51, 42, 3, 70]
df = pd.DataFrame(counts,
                  index=days,
                  columns=['count'],
                 )
df['day of the week'] = df.index.dayofweek

它看起来像这样:

            count   day of the week
2011-01-01  85      5
2011-01-02  97      6
2011-01-03  24      0
2011-01-04  64      1
2011-01-05  3       2
2011-01-06  37      3
2011-01-07  73      4
2011-01-08  86      5
2011-01-09  87      6
2011-01-11  82      1
2011-01-12  75      2
2011-01-13  84      3
2011-01-14  43      4
2011-01-16  51      6
2011-01-18  42      1
2011-01-19  3       2
2011-01-21  70      4

请注意,有些日子会丢失,应该用零填充。我想转换它,所以它看起来像一个日历,以便行增加几周,列是一周中的几天,值是该特定日期的计数。所以最终结果应该是这样的:

    0   1   2   3   4   5   6
0   0   0   0   0   0   85  97
1   24  64  3   37  73  86  87
2   0   82  75  84  0   0   51
3   0   42  3   0   70  0   0 

1 个答案:

答案 0 :(得分:1)

# create weeks number based on day of the week  
df['weeks'] = (df['day of the week'].diff() < 0).cumsum()  

# pivot the table
df.pivot('weeks', 'day of the week', 'count').fillna(0)

enter image description here