pandas Dataframe 中所有行和列的组合

s3fp2yjn  于 2023-04-04  发布在  其他
关注(0)|答案(1)|浏览(103)

给定一个这样的dataframe:

2   6
8   5

我想生成以下列表:

[  [[2],[6,5]],
[[2],[6]],
[[2],[5]],
[[8],[6,5]],
[[8],[6]],
[[8],[5]],
[[6],[2,8]],
[[6],[8]],
[[6],[2]],
[[5],[2,8]],
[[5],[2]],
[[5],[8]],
[[6,5],[2,8]]  ]

我写了以下内容(没有产生预期的结果)

import pandas as pd
from itertools import combinations
import numpy as np
resList=[]
resListTmp=[]
resListTmp2=[]
dataframe = pd.read_excel("C:\\Users\\user\\Desktop\\testData.xlsx",index_col=False,header=None)
for i in range(0, len(dataframe)+1):
    for j in range(0, len(dataframe.columns)):
        for k in range (0,len(dataframe)+1):
            for xVals in list(combinations(dataframe.iloc[k:i,j], i)):
                if list(xVals) not in resListTmp:
                    resListTmp.append(list(xVals))
        resListTmp2.append(resListTmp)
    resList.append(resListTmp2)                
print(resList)

我的代码有什么问题?

nhn9ugyo

nhn9ugyo1#

你没有正确地遍历代码中所有可能的行和列的组合。我已经重写了你的代码,下面这段代码将从 Dataframe 中提取所需的元素,并将它们附加到结果列表中

import pandas as pd
from itertools import combinations, product

# Replace this line with your own data source
data = [[2, 6], [8, 5]]
dataframe = pd.DataFrame(data)

def get_combinations(dataframe):
    res_list = []
    nrows, ncols = dataframe.shape
    
    for r in range(1, nrows+1):
        for c in range(1, ncols+1):
            row_combinations = list(combinations(range(nrows), r))
            col_combinations = list(combinations(range(ncols), c))
            
            for row_comb, col_comb in product(row_combinations, col_combinations):
                tmp = []
                for i in row_comb:
                    tmp.append([dataframe.iloc[i, j] for j in col_comb])
                if tmp not in res_list:
                    res_list.append(tmp)
    return res_list

result = get_combinations(dataframe)
print(result)

相关问题