데이터 프레임이 원하는 크기로 줄어들 때까지 최대 합계로 열을 삭제하는 간단한 재귀 함수를 작성했습니다. 이 위의 코드에서재귀 함수의 중간 결과를 어떻게 목록에 넣으시겠습니까?
s = pd.DataFrame({'a': [1,1,1,1,1,1],
'b': [2,2,2,2,2,2],
'c': [3,3,3,3,3,3],
'd': [4,4,4,4,4,4],
'e': [5,5,5,5,5,5]})
def recSelect(inputdf):
if inputdf.shape[1]<=2:
return inputdf
else:
total = inputdf.sum()
idx = total.idxmax()
inputdf.drop(idx, axis=1, inplace=True)
return recSelect(inputdf)
recSelect(s)
열 'E'를 먼저 제거하고, 그 다음 열 'D', 그리고 'C': 다음 코드이다. 내 질문은 : 어떻게 제대로 'idx'를 반환하고 [ 'e', 'd', 'c']와 같은 목록을 얻는가 ??
는 여기에 내가 시도 무엇 그러나 그것은 작동하지 않습니다 :
idxs = [] # create an empty list
def recSelect(inputdf):
if inputdf.shape[1]<=2:
return inputdf
else:
total = inputdf.sum()
idx = total.idxmax()
idxs.append(idx) # append each idx
inputdf.drop(idx, axis=1, inplace=True)
return recSelect(inputdf), idxs
하, 내가 너무 가까이했다! – user3768495