我try to sort list by frequency and then by name只(Pandas 1.3.2只,Python 3.10只).

首先是I count each occurence in list,然后是if amount is equal, names must be ordered alphabetically.

我发现,当Len(List)<19.魔术...

Code:

import pandas
        
df_data = pandas.DataFrame({
                'data':
                    ['14209adobepremiere', 'adobe-flash-player', 'adobe-flash-player-cis', 
                     'adobe-photoshop-cc-cis', 'discord', 'discord', 'driverpack', 
                     'freeoffice', 'freeoffice2018', 'generals',
                     'tiktok-for-pc-cis', 'tlauncher', 'utorrent', 'viber', 
                     'winrar', 'zoom', 'zoom', 'zoom-client-for-conferences', 
                     'zoom-client-for-conferences-cis']
            })

with pandas.option_context('display.max_rows', None, 'display.max_columns', None):
    print(df_data['data'].value_counts().sort_index(
            ascending=True,
        ).sort_values(ascending=False))

预期输出(按计数顺序,然后按字母顺序顺序):

discord                            2
zoom                               2
14209adobepremiere                 1
adobe-flash-player                 1
adobe-flash-player-cis             1
adobe-photoshop-cc-cis             1
driverpack                         1
freeoffice                         1
freeoffice2018                     1
generals                           1
tiktok-for-pc-cis                  1
tlauncher                          1
utorrent                           1
viber                              1
winrar                             1
zoom-client-for-conferences        1
zoom-client-for-conferences-cis    1
Name: data, dtype: int64

实际输出(按计数说明,而不是按字母顺序):

zoom                               2
discord                            2
14209adobepremiere                 1
tiktok-for-pc-cis                  1
zoom-client-for-conferences        1
winrar                             1
viber                              1
utorrent                           1
tlauncher                          1
generals                           1
adobe-flash-player                 1
freeoffice2018                     1
freeoffice                         1
driverpack                         1
adobe-photoshop-cc-cis             1
adobe-flash-player-cis             1
zoom-client-for-conferences-cis    1
Name: data, dtype: int64

Thnx in advance for any help.

推荐答案

我认为不能先对索引执行.sort_values次操作,然后再对数据进行操作,一种方法可能是重置索引、排序并重新应用索引.

df_data['data'].value_counts()\
        .reset_index().sort_values(['data','index'],
          ascending=[False,True]).set_index('index')

                                data
index
discord                             2
zoom                                2
14209adobepremiere                  1
adobe-flash-player                  1
adobe-flash-player-cis              1
adobe-photoshop-cc-cis              1
driverpack                          1
freeoffice                          1
freeoffice2018                      1
generals                            1
tiktok-for-pc-cis                   1
tlauncher                           1
utorrent                            1
viber                               1
winrar                              1
zoom-client-for-conferences         1
zoom-client-for-conferences-cis     1

Python-3.x相关问答推荐

Pandas groupby基于索引的连续列值相等

估计列表中连续对的数量

向前/向后移动导致移动行的数据不可见

以特定方式重新排列 pandas 数据框的列

在不使用 split 函数的情况下从字符串中分割逗号(','),句号('.')和空格(' '),将字符串的单词附加到列表中

Pandas 转换为日期时间

在特定条件下从 DataFrame 中提取特定组

将 pandas Timestamp() 转换为 datetime.datetime() 以支持 peewee DateTimeField()

如何在数据['column']中的'string'等条件下应用pandas

集合操作:应该只适用于集合,但适用于 dict_keys?

如何在 django 中没有循环的情况下获得前键的前键?

Await Future 来自 Executor:Future 不能在await表达式中使用

python3:字节与字节数组,并转换为字符串和从字符串转换

在 Python 3.5 中使用 aiohttp 获取多个 url

在两个数据框之间查找相等的列

谁能给我一个 Python 3 中标准输入和标准输出的快速教程?

为什么 TensorFlow 的 `tf.data` 包会减慢我的代码速度?

TypeError:多个基地有实例布局冲突

是否可以在每个路由的基础上限制 Flask POST 数据大小?

Python,Docker - ascii编解码器无法编码字符