python-3.x - Pandas 按值和索引对 DF 进行排序

标签 python-3.x pandas dataframe sorting

我正在尝试按频率然后按名称对列表进行排序(pandas 1.3.2,python 3.10)。

首先,我计算列表中的每个出现次数,然后,如果数量相等,则名称必须按字母顺序排序

我发现当 len(list) < 19 时一切正常。魔术...

代码:

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

提前感谢任何帮助。

最佳答案

我认为您不能将 .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 按值和索引对 DF 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73322136/

相关文章:

python - 使用 telegram bot 返回 matplotlib 绘图

python - pandas 没有将类型 string 转换为 float

python - 时间序列的分割 Pandas 数据框

python 列表生成/保存错误

python-3.x - 将 PyFolio 与 Pandas 一起使用

python - 是否可以根据 DataFrame 的值设置 float 的精度?

python - 在 Pandas 中按类别比较

python - 将 Pandas 时间戳插入 Mongodb

python - 仅从列表理解中打印非空数据框

python - 从 Pandas 数据框中提取重复