数据帧中的Python字母排序错误

时间:2018-06-21 07:55:17

标签: python python-3.x sorting alphabetical-sort

我是Python的新手。我需要按字母顺序对我在对象Genre_df中具有的不同流派进行排序,该流派看起来像这样,下面还有更多值 Screenshot of the object Genre_df

但是我无法使用sort_values函数来做到这一点。 我正在这样做:     Genre_df = Genre_df.sort_values('Genre',ascending = False)

它给了我这个错误:

  

KeyError:“流派”。

任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

看起来流派是DF中的索引。

尝试:

Genre_df.sort_index(inplace=True)

答案 1 :(得分:0)

我认为'by'参数有问题,您可以尝试

import pandas as pd
a = pd.DataFrame({
     'Genre': ['Drama', 'Comedy', 'Horror'],
     'Count': [689, 389, 115]
})
a.sort_values(by=['Genre'], ascending=False)