'PYTHON sort a column conditionally by put special char on the top [duplicate]

I am doing my dataset. I need to sort one of my dataset columns from the smallest to the largest like:

enter image description here

however, when I use :

count20 = count20.sort_values(by = ['Month Year', 'Age'])

I got:

enter image description here

Can anyone help me with this? Thank you very much!



Solution 1:[1]

define a function like this:

def fn(x):
    output = []
    for item, value in x.iteritems():
        output.append(''.join(e for e in value if e.isalnum()))
    return output

and pass this function as key while sorting values.

count20 = count20.sort_values(by = ['Month Year', 'Age'], key= fn)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1