簡體   English   中英

從 DataFrame 無重復創建排列

[英]Creating Permutations from DataFrame without Repetition

我已經搜索了這個問題的解決方案,但沒有找到任何特定於這個問題的東西。 我的 dataframe 的結構如下:

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

我想找到上述 dataframe 的所有排列,而無需在每個排列中重復行或列。

前面不是很清楚,所以這里是我試圖實現的 output:

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

換句話說,我期望 n! 結果

我嘗試的解決方案是:

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

這將返回 n^n 組合。

任何幫助表示贊賞! 謝謝

您可以在行索引和 numpy 索引上使用itertools.permutations

from itertools import permutations

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

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

output:

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

您可以使用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