[英]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
的排序值对col1
和col2
、 col3
的列表进行排序以获得以下输出的最有效方法是什么?
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.