pandas - Python PivotTable/Groupby Text Columns -


i'm trying broaden python horizons , learn simple execute in excel.

i have data:

group   function 1   1   b 1   c 2   2   c 3   c 3   3   d 4   e 

and table shows information in format (accomplished pivot table in excel columns: function, rows: group, values: count of group)

      function group        b       c    d    e 1     1        1       1     2     1                1 3     1                1    1 4                                1 

i've created dataframe , added column below:

df = pd.read_excel(filepath) df['1']=1  print(df.groupby('group')) 

but:

1) it's not recognizing function field because it's dtype: object 2) it's not perfomring function i'm looking for, makes me think it's not function need. i've tried work various iterations of pivot_table can't seem work etiher.

does have ideas? in advance.

or using pivot

df1['val']=1 df1.pivot(index='group', columns='function')['val']  function       b    c    d    e group                             1         1.0  1.0  1.0  nan  nan 2         1.0  nan  1.0  nan  nan 3         1.0  nan  1.0  1.0  nan 4         nan  nan  nan  nan  1.0   df1.pivot(index='group', columns='function')['val'].fillna(' ')  function   b  c  d  e group                   1         1  1  1       2         1     1       3         1     1  1    4                     1 

or using groupby :

df1.groupby(['group','function']).size().unstack().fillna(' ') function   b  c  d  e group                   1         1  1  1       2         1     1       3         1     1  1    4                     1 

Comments