![](/img/trans.png)
[英]TypeError: unhashable type: 'numpy.ndarray' when trying to plot a DataFrame
[英]Pandas remove columns from one dataframe that are not in the index of another dataframe - error TypeError: unhashable type: 'numpy.ndarray'
我想從數據框(make_results中的df)獲取索引,並確保只有索引是兩個其他數據框(X_train,X_test)中的列。
但我目前得到錯誤:
TypeError:不可用類型:'numpy.ndarray'
這適用於列表中的多個數據幀。
因此對於這些數據幀: Dataframes X_Train和T_test
我希望他們只有與這些索引匹配的列: 索引
# Remove variables with no relationship from the X_train and X_test data for each make
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]):
# Remove test and training data any insignificant variables
X_train = X_train.filter(df.index.values for df in make_results)
X_test = X_test.filter(df.index.values for df in make_results)
對於數據集X_train和X_test,剩下的唯一列應該是在df.index.values中為make_results中的df索引的列。
如果我理解你想要正確做什么,那么你似乎正在以錯誤的方式使用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)
由於某種原因,我無法在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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.