Filter df to a simple spreadsheet

capnahab :

I have a simple dataframe.


        SITE    OPERTN

152     R1H12    V011
200     RR801    V014
342     RM301    V011
375     RTH08    V011
469     RYJ02    V011
... ... ...
634503  RRK15   V011
634763  RRVNQ   V014
635237  R1H12   V011
635512  RTH08   V011
635548  RJE01   V011
4689 rows × 2 columns

I want to convert this to a simple spreadsheet type table of:

site    V011   V012   V014

R1H12   count  count  count
RR801   count  count  count
RM301   count  count  count
RTH08   count  count  count
etc

I have tried pivoting it, and pivot_table but can't get the agfunc=sum to do it.

AChampion :

You can use pd.crosstab():

pd.crosstab(df['SITE'], df['OPERTN'])

Or df.group_by(), e.g:

df.groupby(['SITE', 'OPERTN']).size().unstack(fill_value=0)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=169974&siteId=1