Below is my data frame:
data = pd.DataFrame([['A',1,15,100,123],['A',2,16,50,7],['A',3,17,100,5],['B',1,20,75,123],['B',2,25,125,7],['B',3,23,100,7],['C',1,5,85,12],['C',2,1,25,6],['C',3,7,100,7]],columns = ['Group','Ranking','Data1','Data2','Correspondence'])
Group Ranking Data1 Data2 Correspondence
0 A 1 15 100 123
1 A 2 16 50 7
2 A 3 17 100 5
3 B 1 20 75 123
4 B 2 25 125 7
5 B 3 23 100 7
6 C 1 5 85 12
7 C 2 1 25 6
8 C 3 7 100 7
I have already sorted the data frame based on 'Group'. However, I still need to sort the data frame based on data for each Group. For each group, Data1 must be sorted based on lowest to highest value and once it is sorted, value in column Data2 will follow the position of Data1. The column Correspondence will not be touched (stay as in original df) and column ranking stays as it is as well. I have used df.sort_values(), but I am unable to get my result as below:
Group Ranking Data1 Data2 Correspondence
0 A 1 15 100 123
1 A 2 16 50 7
2 A 3 17 100 5
3 B 1 20 75 123
4 B 2 23 100 7
5 B 3 25 125 7
6 C 1 1 25 12
7 C 2 5 85 6
8 C 3 7 100 7
So basically my aim is: sort value in Data1 from lowest to highest within each Group, the value in Data2 will follow the movement of Data1 after sorting, while column Correspondence stays where it originally stands.
Thanks.
Group(see index 8)