Pandas 中的 sort_values() 行为与文档相反

我对 Pandas 中 sort_values() 的行为感到困惑,它似乎没有对轴参数做出适当的响应。


以玩具为例:


toy.to_json()

'{"labels":{"0":7,"1":4,"2":7,"3":1,"4":5,"5":0,"6":3,"7":1,"8":4,"9":9},"companies":{"0":"Apple","1":"AIG","2":"Amazon","3":"American express","4":"Boeing","5":"Bank of America","6":"British American Tobacco","7":"Canon","8":"Caterpillar","9":"Colgate-Palmolive"}}'


toy.sort_values('labels') # this works alright

labels  companies

5   0   Bank of America

3   1   American express

7   1   Canon

6   3   British American Tobacco

1   4   AIG

8   4   Caterpillar

4   5   Boeing

0   7   Apple

2   7   Amazon

9   9   Colgate-Palmolive


toy.sort_values(by = 'labels', axis = 1) # Returns an exception

KeyError: 'labels'


皈依舞
浏览 308回答 6
6回答

守候你守候我

这是因为在您的示例中,轴 0 是“向下”,而 1 是“右”(即,跨列)如果您查看sort_values的文档,您会看到第一个参数确实是by,并且默认值axis是0. 所以你重复你的第一个例子,你需要执行toy.sort_values(by='labels', axis=0)

Helenr

在上面的评论和答案中添加一个例子:假设您有一个如下所示的数据框:df = pd.DataFrame(data={"labels":{"0":7,"1":4,"2":7,"3":1,"4":5},"companies":{"0":9,"1":1,"2":6,"3":1,"4":8}})>>df    labels  companies0   7       91   4       12   7       63   1       14   5       8对于axis=0,它会在您传递索引级别和/或列标签时进行排序:df.sort_values(by='labels')它为您提供一个排序的label列(默认情况下升序)。   labels   companies3   1       11   4       14   5       80   7       92   7       6来了axis=1,参考下面的代码:df.sort_values('4',axis=1)这将以排序的方式对列进行index 4排序。在这里它不会改变任何东西,因为 forindex 4因为5小于8并且默认情况下排序是ascending. 但是,如果你执行df.sort_values('1',axis=1)其中值下label超过companies,你将看到的位置labels,并companies已被更换。    companies   labels0   9           71   1           42   6           73   1           14   8           5

繁星coding

只是为了在我们选择axis=1或时了解要清除的轴和行axis=0。df.shape[0]&nbsp; # gives number of row countdf.shape[1]&nbsp; # gives number of col count让我们假设一个数据帧如下:>>> df = pd.DataFrame({...&nbsp; &nbsp; &nbsp;'col1' : ['A', 'A', 'B', np.nan, 'D', 'C'],...&nbsp; &nbsp; &nbsp;'col2' : [2, 1, 9, 8, 7, 4],...&nbsp; &nbsp; &nbsp;'col3': [0, 1, 9, 4, 2, 3],... })>>> df&nbsp; col1&nbsp; col2&nbsp; col30&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;01&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;12&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp;9&nbsp; &nbsp; &nbsp;93&nbsp; NaN&nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp;44&nbsp; &nbsp; D&nbsp; &nbsp; &nbsp;7&nbsp; &nbsp; &nbsp;25&nbsp; &nbsp; C&nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp;3因此,应用 df.shape 并查看它如何围绕列和行旋转:>>> df.shape[0]6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <-- Here, we have six row into the dataFrame>>> df.shape[1]3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <-- Here, we have three columns into the dataFrame现在,如果您只是按列名对值进行排序,则无需指定,axis=1因为已指定列名,您可以简单地执行以下操作:>>> df.sort_values(by=['col1'])&nbsp; col1&nbsp; col2&nbsp; col30&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;01&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;12&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp;9&nbsp; &nbsp; &nbsp;95&nbsp; &nbsp; C&nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp;34&nbsp; &nbsp; D&nbsp; &nbsp; &nbsp;7&nbsp; &nbsp; &nbsp;23&nbsp; NaN&nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp;4或者,您可以将多个列名作为列表传递by:>>> df.sort_values(by=['col1', 'col2'])&nbsp; col1&nbsp; col2&nbsp; col31&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;10&nbsp; &nbsp; A&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;02&nbsp; &nbsp; B&nbsp; &nbsp; &nbsp;9&nbsp; &nbsp; &nbsp;95&nbsp; &nbsp; C&nbsp; &nbsp; &nbsp;4&nbsp; &nbsp; &nbsp;34&nbsp; &nbsp; D&nbsp; &nbsp; &nbsp;7&nbsp; &nbsp; &nbsp;23&nbsp; NaN&nbsp; &nbsp; &nbsp;8&nbsp; &nbsp; &nbsp;4
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python