elena1234

Pivot table and Aggregate function in Python

May 22nd, 2022 (edited)
245
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pivot_city = pd.pivot_table(df,index = ['city'])
  2. pivot_state_take =pd.pivot_table(df,index = ['state','take_out']) # the cells display avg mean values for each 'take_out' in each
  3.  
  4. ############ Aggfunc
  5. pivot_agg = pd.pivot_table(df,index = ['state_id','city_id'], values = ['review_count'],  # specify the column for agg_func
  6.  
  7. columns = ['take_out'], # specify the column to separate the results                        
  8. aggfunc = [np.sum, np.mean])
  9.  
  10. pivot_agg
  11.  
  12. ############
  13. pivot_agg2 = pd.pivot_table(df,index = ['state_id','city_id'],
  14.                             aggfunc = {'review_count':np.sum, 'stars':np.mean})
RAW Paste Data Copied