Chris D'mello
Chris D'mello

Reputation: 155

sort dataframe alphabetically

birth_data.sort_values(by='name',ascending = True) 

I can sort the "numbers" but if i replace it with "name" I receive an error.

KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2441             try:
-> 2442                 return self._engine.get_loc(key)
   2443             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'name'

During handling of the above exception, another exception occurred:

Upvotes: 2

Views: 7762

Answers (1)

jbra95
jbra95

Reputation: 909

Try that:

birth_data.sort_values(by=['name'], ascending = True)

Edit: Are you sure there is a column called 'name'?

Upvotes: 2

Related Questions