26

我有以下 DataFrame,其中包含歌曲名称、他们的峰值图表位置以及他们在第 1 位花费的周数:

                                          Song            Peak            Weeks
76                            Paperback Writer               1               16
117                               Lady Madonna               1                9
118                                   Hey Jude               1               27
22                           Can't Buy Me Love               1               17
29                          A Hard Day's Night               1               14
48                              Ticket To Ride               1               14
56                                       Help!               1               17
109                       All You Need Is Love               1               16
173                The Ballad Of John And Yoko               1               13
85                               Eleanor Rigby               1               14
87                            Yellow Submarine               1               14
20                    I Want To Hold Your Hand               1               24
45                                 I Feel Fine               1               15
60                                 Day Tripper               1               12
61                          We Can Work It Out               1               12
10                               She Loves You               1               36
155                                   Get Back               1                6
8                               From Me To You               1                7
115                              Hello Goodbye               1                7
2                             Please Please Me               2               20
92                   Strawberry Fields Forever               2               12
93                                  Penny Lane               2               13
107                       Magical Mystery Tour               2               16
176                                  Let It Be               2               14
0                                   Love Me Do               4               26
157                                  Something               4                9
166                              Come Together               4               10
58                                   Yesterday               8               21
135                       Back In The U.S.S.R.              19                3
164                         Here Comes The Sun              58               19
96       Sgt. Pepper's Lonely Hearts Club Band              63               12
105         With A Little Help From My Friends              63                7

我想将这些歌曲按受欢迎程度排序,所以我想按照以下标准对它们进行排序:排名最高的歌曲排在第一位,但如果出现平局,则保留在排行榜中的歌曲最长的先到先得。

我似乎无法弄清楚如何在熊猫中做到这一点。

4

6 回答 6

31

在 pandas 0.9.1 及更高版本上,这应该可以工作(这是 0.10.0b1):

编辑:从 Pandas 0.19 开始,方法sort_index已弃用。首选sort_values

In [23]: songs.sort_index(by=['Peak', 'Weeks'], ascending=[True, False])
Out[23]: 
                                      Song  Peak  Weeks
10                           She Loves You     1     36
118                               Hey Jude     1     27
20                I Want To Hold Your Hand     1     24
22                       Can't Buy Me Love     1     17
56                                   Help!     1     17
76                        Paperback Writer     1     16
109                   All You Need Is Love     1     16
45                             I Feel Fine     1     15
29                      A Hard Day's Night     1     14
48                          Ticket To Ride     1     14
85                           Eleanor Rigby     1     14
87                        Yellow Submarine     1     14
173            The Ballad Of John And Yoko     1     13
60                             Day Tripper     1     12
61                      We Can Work It Out     1     12
117                           Lady Madonna     1      9
8                           From Me To You     1      7
115                          Hello Goodbye     1      7
155                               Get Back     1      6
2                         Please Please Me     2     20
107                   Magical Mystery Tour     2     16
176                              Let It Be     2     14
93                              Penny Lane     2     13
92               Strawberry Fields Forever     2     12
0                               Love Me Do     4     26
166                          Come Together     4     10
157                              Something     4      9
58                               Yesterday     8     21
135                   Back In The U.S.S.R.    19      3
164                     Here Comes The Sun    58     19
96   Sgt. Pepper's Lonely Hearts Club Band    63     12
105     With A Little Help From My Friends    63      7
于 2012-12-13T18:24:51.697 回答
23

自 pandas 0.17.0 以来,sort已弃用并替换为sort_values

df.sort_values(['Peak', 'Weeks'], ascending=[True, False], inplace=True)
于 2016-03-03T09:38:20.797 回答
18
df.sort(['Peak', 'Weeks'], ascending=[True, False], inplace=True)

如果您希望排序结果以供将来使用,inplace=True则为必填项。

于 2013-10-20T03:52:08.657 回答
5

通过使用.sort()

df.sort(['Peak', 'Weeks'], ascending=[True, False])

将按峰值位置的升序排序,然后在图表中按长度降序排序。

于 2012-11-29T23:27:02.220 回答
0

如果“Peak”和“Week”的数据类型不是“int”或“float”,则使用以下命令。

df.convert_objects(convert_numeric=True).sort_values(['Peak', 'Weeks'], ascending=[True, False], inplace=True)
于 2016-12-27T10:35:54.040 回答
0

pandas.DataFrame.sort_values df.sort_values(['Peak', 'Weeks'], ascending=[True, False])

请在此找到更多详细信息

于 2021-04-30T03:42:44.930 回答