Using and Graphing The Results of a Crosstab Dataframe in Python

I am trying to do something that should be pretty simple - create a crosstab from some data and then manipulate/graph the results.

Take the following code:

import pandas as pd
import numpy as np
df=pd.read_csv("https://raw.githubusercontent.com/wesm/pydata-   book/master/ch08/tips.csv", sep=',')
df_out=df.pivot_table(index=["day"],values=["tip"], columns=["sex"],aggfunc=[np.sum])

Which gives me a pivot table of tips by day and looks like the following:

enter image description here

The problem is that I need a dataframe that looks like:

enter image description here

So I can interact with is and graph it

For example, I want to do

df[female]-df['male']

and I want to graph a seaborn factor plot of male and female by day

How do I get rid of the extraneous data here? I tried dropping columns, resetting the index, etc, but can't seem to figure it out

Thanks for your help - been fighting with this all day

Upvotes: 0

Views: 1011

Answers (2)

Alex
Alex

Reputation: 12913

Alternate method:

df_out = df_out['sum']['tip']
del df_out.columns.name
del df_out.index.name

Upvotes: 1

Sebastian
Sebastian

Reputation: 1843

I think I remember running into this with other aggregate functions. Will the following work?

new_df = df['sum']['tip']
new_df['delta'] = new_df['female'] - new_df['male']

Upvotes: 1

Related Questions