简体   繁体   English

从 DataFrame 无重复创建排列

[英]Creating Permutations from DataFrame without Repetition

I've searched for a solution to this problem but haven't found anything specific to this problem.我已经搜索了这个问题的解决方案,但没有找到任何特定于这个问题的东西。 My dataframe is structured like this:我的 dataframe 的结构如下:

   column_1    column_2     column_3
a     2           3            7
b     9           4            3
c     1           5            2
        

I want to find all permutations of the above dataframe without repeating rows or columns in each individual permutation.我想找到上述 dataframe 的所有排列,而无需在每个排列中重复行或列。

The preceding isn't super clear, so here is the output I'm trying to achieve:前面不是很清楚,所以这里是我试图实现的 output:

Out: [(2,4,2),(2,5,3),(9,3,2),(9,5,7),(1,3,3),(1,4,7)]

In other words, I expected n!换句话说,我期望 n! results结果

The solution I tried was:我尝试的解决方案是:

permutations = list(product(df['column_1'], df['column_2'], df['column_3']))
print(permutations)

This returns n^n combinations.这将返回 n^n 组合。

Any help is appreciated!任何帮助表示赞赏! THANKS谢谢

You can use itertools.permutations on the row indices and numpy indexing:您可以在行索引和 numpy 索引上使用itertools.permutations

from itertools import permutations

idx = list(permutations(range(len(df))))

df.to_numpy()[idx, np.arange(df.shape[1])].tolist()

output: output:

[[2, 4, 2], [2, 5, 3], [9, 3, 2], [9, 5, 7], [1, 3, 3], [1, 4, 7]]

You can use permutations method of the itertools package. This gives you the indices you need for each column.您可以使用itertools package 的permutations方法。这会为您提供每列所需的索引。

from itertools import permutations
indices = list(permutations('abc', 3))
print(indices)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM