Advertisement
m8_

Check if new column values in groupby of old column values

m8_
Aug 22nd, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. # create some data
  4. data = [['Alex','A','B'],
  5.         ['Alex','C','A'],
  6.         ['Alex','B','A'],
  7.         ['Amanda','A','B'],
  8.         ['Amanda','C','D'],
  9.         ['Amanda','B','A'],
  10.         ['Aaron','A','A'],
  11.         ['Aaron','B','C'],
  12.         ['Aaron','C','A']]
  13.  
  14. # create dataframe
  15. df = pd.DataFrame(data,columns=['Name','Old Grade','New Grade'])
  16.  
  17. # make copies of dataframe
  18. df_old = df.copy()
  19. df_new = df.copy()
  20.  
  21. # transform dataframe with list of old grades for each student
  22. df_old['Old Grades List'] = df_old['Old Grade']
  23. df_old = df_old[['Name','New Grade','Old Grades List']].groupby(['Name'])['Old Grades List'].apply(list).reset_index()
  24.  
  25. # transform dataframe with list of new grades for each student
  26. df_new['New Grades List'] = df_new['New Grade']
  27. df_new = df_new[['Name','New Grade','New Grades List']].groupby(['Name'])['New Grades List'].apply(list).reset_index()
  28.  
  29. # merge dataframes
  30. df_result = pd.merge(df_old, df_new, how="outer", on=["Name"])
  31.  
  32. # add column to check if each new grade in is old grade list
  33. df_result['Match'] = ~(df_result['New Grades List'].apply(set) - df_result['Old Grades List'].apply(set)).astype(bool)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement