Nested dataframe in pandas

Viewed 4002

I have a long list of status codes by month, sth like:

stats = pd.DataFrame(
    [
         ['2016-01', 200, 'xxx.com'],
         ['2016-01', 400, 'xxx.com'],
         ['2016-01', 200, 'xxx.com'],
         ['2016-02', 200, 'xxx.com']
    ],
    columns=['day', 'status_code', 'url']
)

I want to finally plot a few line charts with one line for each status code. I already found out that this table holds the correct information:

pivot = stats.pivot_table(index=['day', 'status_code'], aggfunc=len)

Looks like:

                        url
month   status_code     
2016-01 200            2
        400            1
2016-02 200            1

or as image:

as image

So it's somewhat the information I need.

However:

1.) I already fail at accessing that information. What's e.g. the syntax for getting the number of urls with status code 200 for 2016-01?

2.) How would i plot that? I want to draw multiple lines where x-axis is the month and the y-axis is the status-code-count.

3.) Why is the outer right column named 'url' anyway? I didn't include the url in my pivot table.

1 Answers
Related