I want to sort the following DataFrame on the b-column first and then the a-column.
a | b |
---|---|
0 | 1.2 |
2 | 0.07076863960397785 |
1 | 0.07076863960397783 |
4 | 0.02 |
The math.isclose() function should be used to compare the floats in the b-column. Therefore, I wrote a custom compare function and use the cmp_to_key function from functools. However, when sorting the data frame I get the following error:
TypeError: object of type ‘functools.KeyWrapper’ has no len()
Here’s my full code:
import pandas as pd
from functools import cmp_to_key
from math import isclose
import numpy as np
my_list = [
[0, 1.2],
[2, 0.07076863960397785],
[1, 0.07076863960397783],
[4, 0.02],
[3, 0.07076863960397784]
]
df = pd.DataFrame(my_list,columns=['a','b'])
def compare(a,b):
if isclose(a,b):
return 0
elif a-b<0:
return -1
else:
return 1
df.sort_values(by=['b','a'],key= cmp_to_key(compare))
Now, I know the key in sort_values expects a series, so the key function should be vectorized. But I don’t know how to accomplish this.
This should be the final result:
a | b |
---|---|
4 | 0.02 |
1 | 0.07076863960397783 |
2 | 0.07076863960397785 |
0 | 1.2 |