我尝试使用枢轴表在pivot_table函数的“value”字段中有多个值,但是它不起作用,所以我试着看看是否可以用交叉刀来完成它。这是我的密码
table=pandas.pivot_table(xl2, values='Applications', rows='Sub-Product',cols='Application Date',aggfunc=numpy.sum)当我出口到csv的时候我会得到这个。
Sub-Product 11/1/12 11/2/12 11/3/12
GP 190 207 65
GPF 1391 1430 1269在python中,在将其转换为枢轴表后,dtype为float64(),并使用
<class 'pandas.core.frame.DataFrame'>我最终想要的是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使用类似于此的代码(目前它不工作:/ ):
table=pd.pivot_table(xl2, values=['Acquisitions','Applications'], rows=['Sub-Product'],cols=['Application Date'],aggfunc=np.sum)但我只能得到这个:
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对交叉表有什么帮助的想法吗?
这是csv文件中的数据。我不知道为什么我不能让他们进入适当的数据格式。
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发布于 2013-09-26 04:11:44
看来你离你想去的地方很近了。table.stack(0)将将列索引的第一级移至行索引。
In [1]: import pandas as pd
In [2]: from 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 3https://stackoverflow.com/questions/18986822
复制相似问题