m8_

pandas groupby comparison

m8_
Apr 30th, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.66 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. data = [['Alex',10],['Bob',12],['Clarke',13],['Alex',12],['Bob',18],['Clarke',11],['Alex',10],['Bob',12],['Clarke',13],['Alex',10],['Bob',14],['Clarke',16]]
  4. df = pd.DataFrame(data,columns=['Name','Score'])
  5.  
  6. # create groupby variable that groups Scores by Name
  7. groupby_score = df["Score"].groupby(df["Name"]) #view grouping with list(groupby_score)
  8.  
  9. # loop over each group and and print
  10. for groups, age in groupby_score:
  11.     print(groups, age)
  12.  
  13. # loop to check if difference of all scores in group are within a range of 5
  14. # Ex: Alex's scores are 10, 12, 10, 10. Because the max difference between any two scores is 2, this group is within range
Advertisement
Add Comment
Please, Sign In to add comment