Sort Excel file by column name containing string and integer
<p>I want to sort this Excel file using the second column that is Target. The Target column has data in the form of string and integer</p>

<p><a href="http://i.stack.imgur.com/adjkY.png" rel="nofollow"><img src="http://i.stack.imgur.com/adjkY.png" alt="enter image description here"></a></p>

<p>When I do a sort on the Excel file using the <code>pandas.dataFrame.sort_values()</code> function, I get something like this: </p>

<p><a href="http://i.stack.imgur.com/STwX2.png" rel="nofollow"><img src="http://i.stack.imgur.com/STwX2.png" alt="enter image description here"></a></p>

<p>This sorted order is wrong because Slide2.JPG, Slide3.JPG should be above Slide10.JPG etc. </p>

<p>How do I fix this?</p>

<p>It seems you are looking for <a href="https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/" rel="nofollow">human sorting</a>. You can handle this type of problem using regular expressions in Python. </p>

<p>As explained in the attached article:</p>

<pre><code>import re
def sort_nicely( l ):
    """ Sort the given list in the way that humans expect.
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
    l.sort( key=alphanum_key )

data=["Slide2.JPG","Slide21.JPG","Slide10.JPG","Slide3.JPG"]
sort_nicely(data)
print data
</code></pre>

<p>returns: </p>

<pre><code>['Slide2.JPG', 'Slide3.JPG', 'Slide10.JPG', 'Slide21.JPG']
</code></pre>

python pandas