【发布时间】:2014-08-28 10:32:05
【问题描述】:
复制示例来自:http://tooblippe.github.io/insightstack-blog/2014/04/24/pandas-pivot/
如何配置小部件选择框以允许同时进行多个选择?
%pylab inline
from pandas import Series, DataFrame, pivot_table
import numpy as np
import numpy
from IPython.html.widgets import interact, SelectWidget, CheckboxWidget, RadioButtonsWidget
from IPython.display import display
d = { 'Class' : Series( ['a', 'b', 'b','a','a', 'b', 'b','a','a', 'b', 'b','a','a','b','b','b']),
'Area' : Series( ['North','East', 'South', 'West','North','East', 'South', 'West','North','East', 'South', 'West','South', 'West','South', 'West']),
'Type' : Series( ['square', 'round','square', 'round', 'round', 'square', 'round', 'square', 'round', 'square','round', 'square',]),
'Web' : Series( ['Y','N','N','Y','Y','N','N','Y','Y','N','N','Y','Y','N','N','Y']),
'Agent' : Series( ['Mike', 'John', 'Pete','Mike', 'John', 'Pete','Mike', 'John', 'Pete','Mike', 'John', 'Pete','John', 'Pete','John', 'Pete']),
'Income' : Series( [20., 40., 90., 20.]),
'Profit' : Series( [1., 2., 3., 4.,1., 2., 3., 4.,1., 2., 3., 4.,1., 2., 3., 4.]),
'Stock' : Series( [20., 23., 33., 43.,12., 21., 310., 41.,11., 21., 31., 41.,11., 22., 34., 54.] )
}
df = DataFrame(d)
def my_pivot( rows, values, aggfunc):
dfp = df
piv = pivot_table( dfp, rows=rows, values=values, aggfunc=aggfunc)
print piv
i = interact( my_pivot,
rows = SelectWidget(values=list(df.columns)),
values = SelectWidget(values=['Profit', 'Stock']),
aggfunc = SelectWidget( values={ 'sum' : numpy.sum, 'ave' : numpy.average }))
【问题讨论】:
标签: python pandas ipython ipython-notebook