给定一个这样的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)
我的代码有什么问题?
1条答案
按热度按时间nhn9ugyo1#
你没有正确地遍历代码中所有可能的行和列的组合。我已经重写了你的代码,下面这段代码将从 Dataframe 中提取所需的元素,并将它们附加到结果列表中