Python 如何从数字列表中获取所有可能的排列并存储在数据帧中?

Python 如何从数字列表中获取所有可能的排列并存储在数据帧中?,python,permutation,itertools,Python,Permutation,Itertools,我有一个数字列表,1-6,我需要所有可能的排列,并且需要存储在数据帧中。但是我还需要dataframe将列命名为'Month 1'、'Month 2'、'Month 3'等等 所以我想要这样的东西: Month 1 Month 2 Month 3 Month 4 Month 5 Month 6 1 2 3 4 5 6 1 2 3 4 6 5 1

我有一个数字列表,1-6,我需要所有可能的排列,并且需要存储在数据帧中。但是我还需要dataframe将列命名为'Month 1'、'Month 2'、'Month 3'等等

所以我想要这样的东西:

Month 1  Month 2  Month 3  Month 4  Month 5  Month 6
1        2        3        4        5        6
1        2        3        4        6        5
1        2        3        5        4        6
1        2        3        5        6        4

etc.
我需要存储所有720个可能的排列


我该怎么做?我尝试过使用itertools.permutation,但遇到了问题。

我不确定从置换中获取数据帧有什么困难。这是我的例子

from itertools import permutations
import pandas as pd
h = ['month-{}'.format(i) for i in range(1,7)]
b = [list(i) for i in permutations([i for i in range(1, 7)])]
pd.DataFrame(b, columns=h)

#output 
Out[12]:
     month-1  month-2  month-3  month-4  month-5  month-6
0          1        2        3        4        5        6
1          1        2        3        4        6        5
2          1        2        3        5        4        6
3          1        2        3        5        6        4
4          1        2        3        6        4        5
5          1        2        3        6        5        4
6          1        2        4        3        5        6
from itertools import permutations
a= np.arange(6)
df= pd.DataFrame([comb for comb in permutations(a)],
                 columns=['Month 1','Month 2' ,'Month 3','Month 4','Month 5','Month 6'])
使用itertools.permutations可以执行以下操作:

import pandas as pd
import itertools

a = itertools.permutations([1,2,3,4,5,6])

df = pd.DataFrame(list(a), columns=['Month' + str(i) for i in range(1,7)])
将产生:

Month1  Month2  Month3  Month4  Month5  Month6
0   1   2   3   4   5   6
1   1   2   3   4   6   5
2   1   2   3   5   4   6
3   1   2   3   5   6   4

显示您尝试的内容,然后我们将帮助您修复错误。我们不会为你写这篇文章。请不要回答那些没有表现出他们努力的问题。@Barmar虽然我同意你的观点,即santokiya应该通过发布他尝试的内容来表现出更多的努力,但我认为你不应该阻止那些希望尝试给出答案的人。@ycx如果我们不阻止回答糟糕的问题,我们鼓励提出不好的问题。他需要知道哪些问题值得回答。@Barmar santokiya显然是个新用户。新用户不会通过这种方式学到任何东西imo@ycx我说的是马蒙学习回答什么样的问题。