slaw
slaw

Reputation: 6869

Pivot Daily Time Series to Rows of Weeks in Pandas

I have a Pandas timeseries:

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

And it looks like this:

            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

Notice that there are some days that are missing which should be filled with zeros. I want to convert this so it looks like a calendar so that the rows are increasing by weeks, the columns are days of the week, and the values are the count for that particular day. So the end result should look like:

    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 

Upvotes: 1

Views: 1205

Answers (1)

akuiper
akuiper

Reputation: 214957

# 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

Upvotes: 1

Related Questions