2 回答

TA貢獻1828條經驗 獲得超4個贊
由于某種原因,我無法在 Pandas 中調用列表列表,并且無法編輯元組。最后,我創建了包含數據框和系列的元組列表的副本:
# Drop anything not significant from make_results
for datas in make_results:
datas.drop(datas.loc[datas['P>|z|'] > .05].index, inplace=True)
def remove_others(t, cols):
tuple_list = list(t)
tuple_list[0] = tuple_list[0][cols]
tuple_list[1] = tuple_list[1][cols]
return tuple(tuple_list)
new_train_test_sets = []
list_index = 0
#for df in make_results:
for t in makes_train_test_sets:
new_train_test_sets.append(remove_others(t, make_results[list_index].index.values))
list_index += 1
makes_train_test_sets = new_train_test_sets

TA貢獻1842條經驗 獲得超13個贊
如果我理解您想要正確執行的操作,那么您似乎使用filter了錯誤的方式。
如果您只是想知道如何過濾出作為另一個索引存在的數據框中的列,則需要使用:
X_train.filter(df.index)
如果要遍歷可迭代對象中的所有數據幀并一一過濾:
for X_train, X_test in zip([t[0] for t in makes_train_test_sets],
[t[1] for t in makes_train_test_sets]):
for df in mask_results:
X_train = X_train.filter(df.index)
X_test = X_test.filter(df.index)
您還可以以前“獲取”這些數據幀的所有索引并僅過濾一次:
index_set = set()
for df in mask_results:
index_set = index_set.union(df.index)
for X_train, X_test in zip([t[0] for t in makes_train_test_sets],
[t[1] for t in makes_train_test_sets]):
X_train = X_train.filter(index_set)
X_test = X_test.filter(index_set)
添加回答
舉報