I have a data frame as the following:
df = pd.DataFrame({'id':[3333311,3455572,6464544,2323322,2222111,4333311,5454566,3321767],'A':['12 days','35 days','36 days','56 days','54 days','44 days','56 days','54 days'],'B':['6 days','31 days','33 days','46 days','44 days','16 days','41 days','42 days'],'Percentage':[0.41,0.36,0.36,0.42,0.25,0.56,0.25,0.42]})
id A B Percentage
1 3333311 12 days 6 days 0.41
3953 3455572 35 days 31 days 0.36
46458 6464544 36 days 33 days 0.36
39378 2323322 56 days 46 days 0.42
115880 2222111 54 days 44 days 0.25
115882 4333311 44 days 16 days 0.56
118882 5454566 56 days 41 days 0.25
118884 3321767 54 days 42 days 0.42
I want to sort it first on Percentage.
Then when a tie breaker comes, it should sort on A and B simultaneously as
if A.iloc[1] < A.iloc[2]
and B.iloc[1] < B.iloc[2]
then df.iloc[2] should come first and vice versa.
But if A.iloc[1] < A.iloc[2]
and B.iloc[1] > A.iloc[2]
or if A.iloc[1] > A.iloc[2]
and B.iloc[1] < A.iloc[2]
I have tried this but not getting exact result
df = df.sort_values(by='B').sort_values(by='A').sort_values(by='Percentage', ascending=False)
Expected output will be like this:
id A B Percentage
115882 4333311 44 days 16 days 0.56
39378 2323322 56 days 46 days 0.42
118884 3321767 54 days 42 days 0.42
1 3333311 12 days 6 days 0.41
46458 6464544 36 days 33 days 0.36
3953 3455572 35 days 31 days 0.36
118882 5454566 56 days 41 days 0.25
115880 2222111 54 days 44 days 0.25
Now here on 0.25 percentage tie breaker, there is the condition: if A.iloc[118882] > A.iloc[115880] and B.iloc[118882] < B.iloc[115880]
Here we will perform other operations and consider other aggregations.
Can we do it by sorted function with cmp Parameter?