2016-12-08 69 views
0

我想要做一些應該非常簡單的事情 - 從一些數據創建交叉表,然後操縱/繪製結果。在Python中使用並繪製交叉表數據框的結果

看看下面的代碼:

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]) 

這一天給我提示的數據透視表,看起來像下面這樣:

enter image description here

的問題是,我需要一個數據幀是看起來像:

enter image description here

這樣我就可以是和圖形它

例如,我想要做的

df[female]-df['male'] 

互動,我想白天到圖形男性和女性的seaborn因素情節

怎麼辦我在這裏擺脫了無關的數據?我試着刪除列,重置索引,等等,但似乎無法推測出來

感謝您的幫助 - 被這個整天

回答

1

我想我記得運行到這個與其它集合函數戰鬥。以下的工作?

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

替代方法:

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