找到所有可能的配对并存储在list of list of list中



Names        Henry    Adam   Rachel  Jug   Jesscia  May   Peter
Robert         54      0        0     6       5      8      24
Dan            22      31       0     0       55     4       8

我想在下面的df中找到所有可能的具有数值的列对(Henry, Adam, Rachel, Jug, Jesscia, May, Peter,),并将它们的值附加为列表的列表的列表的列表。

例如:

[[[54,22],[0,31]], [[54,22],[6,0]]......] 

,其中[54,22],[0,31]是第一对我将非常感谢任何帮助!

我们可以select_dtypes只获得数字列,然后transpose(T)并将DataFramevalues转换为列表。最后,使用permutations获取长度为2的所有可能的对:

from itertools import permutations
import pandas as pd
df = pd.DataFrame({
'Names': ['Robert', 'Dan'], 'Henry': [54, 22], 'Adam': [0, 31],
'Rachel': [0, 0], 'Jug': [6, 0], 'Jesscia': [5, 55], 'May': [8, 4],
'Peter': [24, 8]
})
output = list(
map(list,
permutations(df.select_dtypes(include='number').T.values.tolist(), 2)
)
)

output:

[[[54, 22], [0, 31]],
[[54, 22], [0, 0]],
[[54, 22], [6, 0]],
[[54, 22], [5, 55]],
[[54, 22], [8, 4]],
[[54, 22], [24, 8]],
[[0, 31], [54, 22]],
[[0, 31], [0, 0]],
[[0, 31], [6, 0]],
[[0, 31], [5, 55]],
[[0, 31], [8, 4]],
[[0, 31], [24, 8]],
[[0, 0], [54, 22]],
[[0, 0], [0, 31]],
[[0, 0], [6, 0]],
[[0, 0], [5, 55]],
[[0, 0], [8, 4]],
[[0, 0], [24, 8]],
[[6, 0], [54, 22]],
[[6, 0], [0, 31]],
[[6, 0], [0, 0]],
[[6, 0], [5, 55]],
[[6, 0], [8, 4]],
[[6, 0], [24, 8]],
[[5, 55], [54, 22]],
[[5, 55], [0, 31]],
[[5, 55], [0, 0]],
[[5, 55], [6, 0]],
[[5, 55], [8, 4]],
[[5, 55], [24, 8]],
[[8, 4], [54, 22]],
[[8, 4], [0, 31]],
[[8, 4], [0, 0]],
[[8, 4], [6, 0]],
[[8, 4], [5, 55]],
[[8, 4], [24, 8]],
[[24, 8], [54, 22]],
[[24, 8], [0, 31]],
[[24, 8], [0, 0]],
[[24, 8], [6, 0]],
[[24, 8], [5, 55]],
[[24, 8], [8, 4]]]

最新更新