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条经验 获得超12个赞
如果我理解您想要正确执行的操作,那么您似乎使用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)
添加回答
举报