Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- # create some data
- data = [['Alex','A','B'],
- ['Alex','C','A'],
- ['Alex','B','A'],
- ['Amanda','A','B'],
- ['Amanda','C','D'],
- ['Amanda','B','A'],
- ['Aaron','A','A'],
- ['Aaron','B','C'],
- ['Aaron','C','A']]
- # create dataframe
- df = pd.DataFrame(data,columns=['Name','Old Grade','New Grade'])
- # make copies of dataframe
- df_old = df.copy()
- df_new = df.copy()
- # transform dataframe with list of old grades for each student
- df_old['Old Grades List'] = df_old['Old Grade']
- df_old = df_old[['Name','New Grade','Old Grades List']].groupby(['Name'])['Old Grades List'].apply(list).reset_index()
- # transform dataframe with list of new grades for each student
- df_new['New Grades List'] = df_new['New Grade']
- df_new = df_new[['Name','New Grade','New Grades List']].groupby(['Name'])['New Grades List'].apply(list).reset_index()
- # merge dataframes
- df_result = pd.merge(df_old, df_new, how="outer", on=["Name"])
- # add column to check if each new grade in is old grade list
- 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