代码之家  ›  专栏  ›  技术社区  ›  josepmaria

Pandas顺序列,按对列出

  •  1
  • josepmaria  · 技术社区  · 10 月前

    以下是数据帧:

    df1 = pd.DataFrame( {'st': {0: 1, 1: 0, 2: 2, 3: 0, 4: 1, 5: 5, 6: 0, 7: 7, 8: 19, 9: 0, 10: 0, 11: 0, 12: 3, 13: 0}, 'gen': {0: 'B1', 1: 'A0,B0', 2: 'A1,B1', 3: 'A0,B0', 4: 'B109', 5: 'B4,A1', 6: 'A0,B0', 7: 'A4,B3', 8: 'B15,A4', 9: 'A0,B0', 10: 'A0,B0', 11: 'A0,B0', 12: 'A123', 13: 'A0,B0'}, 'gen2': {0: 'B(1)', 1: 'A(0),B(0)', 2: 'A(1),B(1)', 3: 'A(0),B(0)', 4: 'B(109)', 5: 'A(1),B(4)', 6: 'A(0),B(0)', 7: 'A(4),B(3)', 8: 'A(4),B(15)', 9: 'A(0),B(0)', 10: 'A(0),B(0)', 11: 'A(0),B(0)', 12: 'A(123)', 13: 'A(0),B(0)'}} )
    

    它产生:

        st  gen     gen2
    0   1   B1      B(1)
    1   0   A0,B0   A(0),B(0)
    2   2   A1,B1   A(1),B(1)
    3   0   A0,B0   A(0),B(0)
    4   1   B109    B(109)
    5   5   B4,A1   A(1),B(4)
    6   0   A0,B0   A(0),B(0)
    7   7   A4,B3   A(4),B(3)
    8   19  B15,A4  A(4),B(15)
    9   0   A0,B0   A(0),B(0)
    10  0   A0,B0   A(0),B(0)
    11  0   A0,B0   A(0),B(0)
    12  3   A123    A(123)
    13  0   A0,B0   A(0),B(0)
        
    

    请注意,列['gen2']是所寻求的结果

    以昏迷分隔,列[“gen”]表示值对。每个值对由一个字母(a或B)和一个数字(整数)组成。

    我希望['gen']列按对顺序显示结果,先输入“A+值”,然后输入“B+值”。另请注意,['gen1']表示括号中字母后的所有值。

    请参阅搜索结果栏,其中索引编号5和14发生了变化。

    Index 5, column['gen2'] reorders column ['gen'] from B4,A1 to **A(1),B(4)**.
    
    Index 8, column['gen2'] reorders column ['gen'] from B14,A4 to **A(4),B(15)**.
    
    2 回复  |  直到 10 月前
        1
  •  2
  •   mozway    10 月前

    使用一个简短的自定义函数来提取字母/数字,对数字进行排序,并重建一个正确的字符串:

    from natsort import natsorted
    import re
    
    def reorder(s):
        return ','.join(f'{c}({d})' for c,d in
                        natsorted(re.findall(r'([A-Z])(\d+)', s)))
    
    df1['gen2'] = df1['gen'].apply(reorder)
    

    NB。 natsorted 是可选的,如果您有多个,这也是按数字排序 A B 如果你只有一个 A. / B 你可以直接使用 sorted :

    import re
    
    def reorder(s):
        return ','.join(f'{c}({d})' for c,d in
                        sorted(re.findall(r'([A-Z])(\d+)', s)))
    
    df1['gen2'] = df1['gen'].apply(reorder)
    

    输出:

        st     gen        gen2
    0    1      B1        B(1)
    1    0   A0,B0   A(0),B(0)
    2    2   A1,B1   A(1),B(1)
    3    0   A0,B0   A(0),B(0)
    4    1    B109      B(109)
    5    5   B4,A1   A(1),B(4)
    6    0   A0,B0   A(0),B(0)
    7    7   A4,B3   A(4),B(3)
    8   19  B15,A4  A(4),B(15)
    9    0   A0,B0   A(0),B(0)
    10   0   A0,B0   A(0),B(0)
    11   0   A0,B0   A(0),B(0)
    12   3    A123      A(123)
    13   0   A0,B0   A(0),B(0)
    
        2
  •  0
  •   stkovrflwanuurage    10 月前

    要在pandas DataFrame中对每个单元格都包含值列表的列进行排序,可以按对对列表进行排序。例如,如果你有一个DataFrame,其中每个单元格都包含一个元组(对)列表,并且你想根据每个元组中的第一个值对这些列表进行排序,你可以通过对每个单元格应用自定义函数来实现。

    例子

    假设您有以下DataFrame:

    import pandas as pd
    
    data = {
        'A': [['b', 'a'], ['d', 'c'], ['f', 'e']],
        'B': [[(2, 'y'), (1, 'x')], [(4, 'w'), (3, 'v')], [(6, 'u'), (5, 't')]]
    }
    
    df = pd.DataFrame(data)
    print(df)
    

    输出:

           A                 B
    0  [b, a]  [(2, 'y'), (1, 'x')]
    1  [d, c]  [(4, 'w'), (3, 'v')]
    2  [f, e]  [(6, 'u'), (5, 't')]
    

    现在,假设您想按每个元组的第一个元素对列“B”中的列表进行排序。您可以这样做:

    按对对列排序

    # Define a function to sort lists of pairs by the first element of each tuple
    def sort_by_first_element(pairs):
        return sorted(pairs, key=lambda x: x[0])
    
    # Apply the sorting function to each cell in column 'B'
    df['B'] = df['B'].apply(sort_by_first_element)
    
    print(df)
    

    输出

           A                 B
    0  [b, a]  [(1, 'x'), (2, 'y')]
    1  [d, c]  [(3, 'v'), (4, 'w')]
    2  [f, e]  [(5, 't'), (6, 'u')]
    

    解释

    • 自定义排序功能: sort_by_first_element() 函数根据每个元组中的第一个元素对一系列对(元组)进行排序。
    • apply() 方法: .apply() 方法用于将此函数应用于“B”列中的每个单元格。

    此方法可以根据您对列表的排序方式进行调整(例如,按元组的第二个元素或降序排列)。调整 key sorted() 根据需要发挥作用。