Hi everyone I have tried to look everywhere for this issue but I cannot figure a solution out. I'd be glad if you'd help me.
So, basically I have this dataset:
df = pd.DataFrame({"col1": ['xxx', 'xxx', 'xxx', 'kkk', 'www', 'www'],
"col2": [ 2020, 1994, 2013, 1000, 1996, 2021]})
df.dtypes
col1 object
col2 int64
dtype: object
and I want to order the first column with a costum order and the second column with ascending order. The final result should be the following:
col1 col2
4 www 1996
5 www 2021
3 kkk 1000
0 xxx 1994
1 xxx 2013
2 xxx 2020
So, in order to accomplish that I do this:
d = {'xxx': 4, 'zzz':1, 'yyy':5, 'kkk':2, 'jjj':3, 'www':0} # to customize order
df.sort_values(by = ['col1' , 'col2'], key = lambda x: x.map(d))
but I end up with this:
col1 col2
4 www 1996
5 www 2021
3 kkk 1000
0 xxx 2020
1 xxx 1994
2 xxx 2013
If I only do:
df.sort_values(by = ['col1' , 'col2'])
col1 col2
3 kkk 1000
4 www 1996
5 www 2021
1 xxx 1994
2 xxx 2013
0 xxx 2020
The col2 is ordered fine. I really don't know why I am having this issue. Has anyone experienced something similar? Thanks