简体   繁体   中英

pivot a table in python using pandas

I have a table as below:(currently this table is filtered to show only 1 visitor)

vstid vstrseq  date       page   timespent

1       1     1/1/16      a       20.00
1       1     1/1/16      b       3.00
1       1     1/1/16      c       131.00
1       1     1/1/16      d        .000
1       1     1/1/16      a       3.00

i want this like:

vstid   vstrseq      date      a   b   c   d
1        1          1/1/16    23   3  131  0

i tried to create a dataframe like below and tried to pivot it:

ptable=pd.DataFrame(table,columns= ['vstid','vstrseq','date','page','timespent'])
pvtable=pd.pivot_table(ptable,index='vstid','vstrseq','date'],columns='page',values='timespent',aggfunc=np.sum)

I got error msg like below:

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3824)()

pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3704)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12280)()

pandas/hashtable.pyx in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12231)()

KeyError: 'TIMESPENT'`

You had just a bracket missing at your index definition:

import pandas as pd
import numpy as np

df2 = pd.DataFrame({ 'vstid' : 1.,
                     'vstrseq' : 1, 
                     'date' :  "1/1/16",
                     'page' : pd.Categorical(["a","b","c","d", "a"]),
                     'timespent' : pd.Categorical([20.00,3.0, 131.0,0.0, 3.0])})
print df2  

df3=pd.pivot_table(df2,index=['vstid','vstrseq','date'],columns='page',values='timespent',aggfunc=np.sum)

print df3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM