How to read pivot table from excel document in python pandas? -


i have 1 excel document contains sport column, in sports name , sports persons names available. if clicked on sports name sports persons names disappears i.e. sports persons names children's of sports name.

please @ data below: enter image description here

if clicked on cricket ramesh, suresh,mahesh names disappears i.e. cricket parent of ramesh, suresh , mahesh same football parent of pankaj, riyansh, suraj.

i want read excel document , convert in python pandas dataframe. tried read pandas pivot_table i'm not getting success.

i tried read excel sheet , converted dataframe.

df = pd.read_excel("sports.xlsx",skiprows=7,header=0) d = pd.pivot_table(df,index=["sports"]) print d 

but i'm getting sports values in single column want split sports name , it's corresponding sports persons name.

expected output:

sports_name player_name   age address cricket     ramesh        20  aaa cricket     suresh        21  bbb cricket     mahesh        22  ccc football    pankaj        24  eee football    riyansh       25  fff football    suraj         26  ggg basketball  rajesh        28  iii basketball  abhijeet      29  jjj 

pandas.pivot_table there support data analysis , helps create pivot tables similar excel, not read excel pivot tables.

create spreadsheet-style pivot table dataframe. levels in pivot table stored in multiindex objects (hierarchical indexes) on index , columns of result dataframe

example documentation

>>> df      b   c      d 0  foo 1 small  1 1  foo 1 large  2 2  foo 1 large  2 3  foo 2 small  3 4  foo 2 small  3 5  bar 1 large  4 6  bar 1 small  5 7  bar 2 small  6 8  bar 2 large  7   >>> table = pivot_table(df, values='d', index=['a', 'b'], ...                     columns=['c'], aggfunc=np.sum) >>> table           small  large foo  1  1      4      2  6      nan bar  1  5      4      2  6      7 

now on problem, created sample data set , pivot table.

then read excel sheet pandas dataframe. dataframe contains nans replaced using df.fillna(method='ffill')

enter image description here

df = pd.read_excel(pviotfile,skiprows=12,header=0) df=df.fillna(method='ffill') print (df) 

output

       sports     name  address  age 0  basketball  abhijit  129 abc   20 1  basketball   rajesh  128 abc   20 2     cricket   mahesh  123 abc   20 3     cricket   ramesh  126 abc   20 4     cricket   suresh  124 abc   20 5    football   riyash  125 abc   20 6    football    suraj  127 abc   20 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -