【问题标题】:How can I sort strings in pandas data file according to the value in specific column? [duplicate]如何根据特定列中的值对 pandas 数据文件中的字符串进行排序? [复制]
【发布时间】:2021-03-14 09:25:28
【问题描述】:

我的 Python 程序生成一个 pandas 格式的数据文件

        Source    LogP    MolWt  HBA  HBD
0        cne_1  1.1732  263.405    3    1
1       cne_10  2.6639  197.237    2    0
2      cne_100 -0.2886  170.193    4    2
3     cne_1000  1.9644  304.709    5    1
4     cne_1001  1.4986  162.144    3    1
...        ...     ...      ...  ...  ...
1031   cne_995  3.0179  347.219    4    2
1032   cne_996  4.8419  407.495    6    2
1033   cne_997  3.3560  354.524    3    1
1034   cne_998  7.5465  635.316    4    2
1035   cne_999  3.3514  389.556    4    1

我需要根据第二列(Source)根据数字对字符串进行排序,所以排序后的行的正确顺序应该是:cne_1,cne_2,cne_3,cne_4 et 我试过用:

df_sorted = df.sort_values('Source', ascending=True)

但它并没有对行的顺序产生任何变化。

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    对于最后的 pandas 版本,可以使用参数 key_ 拆分值并将值转换为整数:

    df_sorted = df.sort_values('Source', key=lambda x: x.str.split('_').str[1].astype(int)) 
    

    或者可以通过Series.argsort获取排序值的位置并传递给DataFrame.iloc

    df_sorted = df.iloc[df['Source'].str.split('_').str[1].astype(int).argsort()]
    print (df_sorted)
            Source    LogP    MolWt  HBA  HBD
    0        cne_1  1.1732  263.405    3    1
    1       cne_10  2.6639  197.237    2    0
    2      cne_100 -0.2886  170.193    4    2
    1031   cne_995  3.0179  347.219    4    2
    1032   cne_996  4.8419  407.495    6    2
    1033   cne_997  3.3560  354.524    3    1
    1034   cne_998  7.5465  635.316    4    2
    1035   cne_999  3.3514  389.556    4    1
    3     cne_1000  1.9644  304.709    5    1
    4     cne_1001  1.4986  162.144    3    1
    

    【讨论】:

      【解决方案2】:

      获取列中的整数值,然后使用它进行排序。

      df['sortIndex'] = df.Source.str.replace('cne_', '', regex=False).astype(int)
      df_sorted = df.sort_values('sortIndex', ascending=True)
      

      【讨论】:

        【解决方案3】:

        提取数字,转换为 int 并进行相应的排序。 (.sort_values(0) 因为未命名的列自动命名为0

        df_sorted = df.loc[df["Source"].str.extract(r"_(\d+)").astype(int).sort_values(0).index]
        

        结果

        print(df_sorted)
                Source    LogP    MolWt  HBA  HBD
        0        cne_1  1.1732  263.405    3    1
        1       cne_10  2.6639  197.237    2    0
        2      cne_100 -0.2886  170.193    4    2
        1031   cne_995  3.0179  347.219    4    2
        1032   cne_996  4.8419  407.495    6    2
        1033   cne_997  3.3560  354.524    3    1
        1034   cne_998  7.5465  635.316    4    2
        1035   cne_999  3.3514  389.556    4    1
        3     cne_1000  1.9644  304.709    5    1
        4     cne_1001  1.4986  162.144    3    1
        

        【讨论】:

          猜你喜欢
          • 2020-05-19
          • 2016-10-08
          • 1970-01-01
          • 2016-11-18
          • 2021-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多