Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. # Groupby method
  2. # split the data into groups based on some criteria
  3. # calculate statistics or apply a function to each group
  4. # group data using rank
  5. df_rank = df.groupby(['rank'])
  6.  
  7. # Calculate mean value for each numneric column per each group
  8. df_rank.mean()
  9.  
  10. # Calculate mean salary for each professor rank
  11. df.groupby('rank')[['salary']].mean()
  12.  
  13. # groupby performance notes:
  14. # - no grouping/splitting occurs unit it's needed. Creating the groupby object only verifies that you have passed a valid mapping
  15. # - by default, the group keys are sorted during the groupby operation. You may want to pass sort=false for potential speedup
  16. # Calculate mean salary for each professor rank:
  17. df.groupby(['rank'],sort=False)[['salary']].mean()
  18.  
  19. # FILTERING
  20. # Calculate salary greater than 120000
  21. df_sub = df[df['salary']>120000]
  22. print(df_sub)
  23.  
  24. # Select only those rows that contain female professors:
  25. df_f = df[df['sex']=='Female']
  26. print(df_f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement