python - How to use crosstab/pivot with multi dimensions -
i tried using pivot tables have more 1 values in 'values' field pivot_table function doesnt work, im trying see if can crosstabs. here code
table=pandas.pivot_table(xl2, values='applications', rows='sub-product',cols='application date',aggfunc=numpy.sum)
i when export csv.
sub-product 11/1/12 11/2/12 11/3/12 gp 190 207 65 gpf 1391 1430 1269
in python, after converting pivot table, dtype float64() , using
<class 'pandas.core.frame.dataframe'>
what want output in csv:
row labels 11/1/2012 11/2/2012 11/3/2012 gp acquisitions 164 168 54 applications 190 207 65 gpf acquisitions 1124 1142 992 applications 1391 1430 1269
with code along lines of (currently doesnt work :/ ) :
table=pd.pivot_table(xl2, values=['acquisitions','applications'], rows=['sub-product'],cols=['application date'],aggfunc=np.sum)
but can this:
sub-product ('applications', timestamp('2012-11-01 00:00:00', tz=none)) ('applications', timestamp('2012-11-02 00:00:00', tz=none)) ('applications', timestamp('2012-11-03 00:00:00', tz=none)) gp 190 207 65 gpf 1391 1430 1269
any ideas on how crosstab can help? :s
this data in csv file. i'm not sure why cant them proper dataframe format.
application date sub-product applications acquisitions 11/1/12 gp 1 1 11/1/12 gp 1 1 11/1/12 gp 1 1 11/1/12 gp 1 1 11/1/12 gpf 1 1 11/1/12 gpf 1 1 11/1/12 gpf 1 1 11/1/12 gpf 1 1
looks you're close want be. table.stack(0)
move first level of column index row index.
in [1]: import pandas pd in [2]: stringio import stringio in [3]: df = pd.read_csv(stringio("""\ ...: application-date sub-product applications acquisitions ...: 11/1/12 gp 1 1 ...: 11/1/12 gpf 1 1 ...: 11/2/12 gp 1 1 ...: 11/2/12 gp 1 1 ...: 11/2/12 gpf 1 1 ...: 11/2/12 gpf 1 1 ...: 11/3/12 gp 1 1 ...: 11/3/12 gp 1 1 ...: 11/3/12 gp 1 1 ...: 11/3/12 gpf 1 1 ...: 11/3/12 gpf 1 1 ...: 11/3/12 gpf 1 1 ...: """), sep='\s+', parse_dates=[0]) in [4]: table = df.pivot_table(values=['acquisitions', 'applications'], ...: rows='sub-product', ...: cols='application-date', ...: aggfunc=sum) in [5]: table out[5]: applications acquisitions application-date 2012-11-01 2012-11-02 2012-11-03 2012-11-01 2012-11-02 2012-11-03 sub-product gp 1 2 3 1 2 3 gpf 1 2 3 1 2 3 in [6]: table.stack(0) out[6]: application-date 2012-11-01 2012-11-02 2012-11-03 sub-product gp applications 1 2 3 acquisitions 1 2 3 gpf applications 1 2 3 acquisitions 1 2 3
Comments
Post a Comment