简体   繁体   English

Pandas:根据另一个列列表中的值对列列表进行排序

[英]Pandas: sort column lists based on values from another column list

Given a dataframe like this:给定一个这样的数据框:

id   col1     col2           col3
------------------------------------------
1    [2,3,1]  ['a','b','c']  ['d','e','f']
2    [3,2,1]  ['a','b','c']  ['d','e','f']

What is the most efficient way to sort the lists in col1 and col2 , col3 using the sorted values from col1 to get the following output?使用col1的排序值对col1col2col3的列表进行排序以获得以下输出的最有效方法是什么?

id   col1     col2           col3
------------------------------------------
1    [1,2,3]  ['c','a','b']  ['f','d','e']
2    [1,2,3]  ['c','b','a']  ['f','e','d']

Thanks.谢谢。

You can try this:你可以试试这个:

df = pd.DataFrame({'col1':[ [2,3,1], [3,2,1]  ],
                   'col2':[ ['a','b','c'], ['a','b','c'] ],
                   'col3':[ ['d','e','f'], ['d','e','f'] ]})

def custom_sort(x):
    col1 = sorted(enumerate(x.col1), key=lambda k: k[1])
    col2 = [x.col2[i] for i, _ in col1]
    col3 = [x.col3[i] for i, _ in col1]
    return [v for _, v in col1], col2, col3

df[['col1', 'col2', 'col3']] = df[['col1', 'col2', 'col3']].apply(custom_sort, axis=1, result_type='expand')
print(df)

Prints:印刷:

        col1       col2       col3
0  [1, 2, 3]  [c, a, b]  [f, d, e]
1  [1, 2, 3]  [c, b, a]  [f, e, d]

I would use numpy argsort on col1 and use fancy index on each columns using apply我会在col1上使用 numpy argsort并使用apply在每列上使用花哨的索引

m = np.array(df.col1.tolist()).argsort()
i_0 = np.arange(df.shape[0])[:,None]    
df[['col1','col2','col3']] = df[['col1','col2','col3']].apply(lambda x: 
                                            np.array(x.tolist())[i_0, m].tolist())

Out[1700]:
   id       col1       col2       col3
0   1  [1, 2, 3]  [c, a, b]  [f, d, e]
1   2  [1, 2, 3]  [c, b, a]  [f, e, d]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从基于另一个列表的列表列中删除列表值 - Remove list values from a column of lists based on another list 如果熊猫数据框列中存在列表值列表,请用另一个熊猫列中的值替换它们 - If list of lists values are present in Pandas dataframe column replace them with values from another Pandas column 根据另一个列表类型列对 pandas 列表类型列值进行排序 - Sort pandas list type column values based on another list type column 基于另一列的值列表的一列中的 Pandas 查找值 - Pandas lookup values in one column based on list of values of another column 根据另一列的值在pandas的列中创建值列表 - create a list of values in a column in pandas based on values of another column 从 pandas 列表列中,如何删除不在列表另一列中的所有值? - From a pandas column of lists, how to remove all values not in another column of list? 根据另一个pandas数据框中的列排序对列进行排序 - Sort a column based on the sorting of a column from another pandas data frame Pandas:根据另一列的值从一列中提取值 - Pandas: Extract values from a column based on the values from another column Pandas:根据另一列中的索引列表添加一列来自其他列的值列表 - Pandas: Add a column of list of values from other columns based on an index list in another column Select 列动态在 Pandas dataframe 基于列表或另一列中的值 - Select column dynamically in Pandas dataframe based on values in a list or another column
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM