Given a pandas dataframe, each row in the dataframe should be duplicated, but the duplicate row differs from the original row in terms of the score1 and score2 columns whereby their values are swapped. Furthermore, the incremental nature of unique ID must be kept.
Original dataframe:
nme = ["Tom", "John", "Philip", "Ross"]
deg = ["MBA", "BCA", "M.Tech", "MBA"]
scr1 = [90, 40, 80, 98]
scr2 = [10, 20, 30, 50]
# dictionary of lists
dct = {'name': nme, 'degree': deg, 'score1': scr1, 'score2': scr2}
my_dataframe = pd.DataFrame(dct)
print(my_dataframe)
name degree score1 score2
0 Tom MBA 90 10
1 John BCA 40 20
2 Philip M.Tech 80 30
3 Ross MBA 98 50
Expected dataframe:
nme = ["Tom","Tom", "John", "John", "Philip","Philip", "Ross", "Ross"]
deg = ["MBA", "MBA", "BCA", "BCA", "M.Tech", "M.Tech", "MBA", "MBA"]
scr1 = [90, 10, 40, 20, 80, 30, 98, 50]
scr2 = [10, 90, 20, 40, 30, 80, 50, 98]
# dictionary of lists
dct = {'name': nme, 'degree': deg, 'score1': scr1, 'score2': scr2}
my_new_dataframe = pd.DataFrame(dct)
print(my_new_dataframe)
name degree score1 score2
0 Tom MBA 90 10
1 Tom MBA 10 90
2 John BCA 40 20
3 John BCA 20 40
4 Philip M.Tech 80 30
5 Philip M.Tech 30 80
6 Ross MBA 98 50
7 Ross MBA 50 98
Thank you very much in advance.