group_and_count.py 843 B

12345678910111213141516171819202122232425
  1. #! /usr/bin/env python3
  2. # playing with group and count
  3. # https://stackoverflow.com/questions/38174155/group-dataframe-and-get-sum-and-count
  4. import pandas as pd
  5. data = {
  6. 'Company' : ['Ford','Ford','Ford','Ford','Chevy','Chevy'],
  7. 'Type' : ['Mercury','Lincoln','Lincoln','Econoline','Malabu','Pickups'],
  8. 'Profit' : [1,100,40,99,2,3]
  9. }
  10. df = pd.DataFrame(data)
  11. # Want to compute mean and std of Profit
  12. # Add duplicates of value to be aggrigated
  13. df['mean_profit'] = df["Profit"]
  14. df['std_profit'] = df["Profit"]
  15. df2 = df.groupby('Company').agg({'Type':'count','Profit':'sum', 'mean_profit':'mean','std_profit':'std'}).reset_index().rename(columns={'Type':"count"})
  16. print(df2)
  17. # Company count Profit mean_profit std_profit
  18. # 0 Chevy 2 5 2.5 0.707107
  19. # 1 Ford 4 240 60.0 48.311489