我想使用python从dataframe中删除多个列(大约800个)。我写了以下代码:def corr_df(x, corr_val):
# Creates Correlation Matrix and Instantiates
corr_matrix = x.corr()
iters = range(len(corr_matrix.columns) - 1)
drop_cols = []
df_drop=pd.DataFrame()
cols=[]
# Iterates through Correlation Matrix Table to find correlated columns
for i in iters:
for j in range(i):
item = corr_matrix.iloc[j:(j+1), (i+1):(i+2)]
col = item.columns
row = item.index
val = item.values
if val >= corr_val:
# Prints the correlated feature set and the corr val
#print(col.values[0], "|", row.values[0], "|", round(val[0][0], 2))
drop_cols.append(i)
drops = sorted(set(drop_cols))[::-1]
df_dropped=x.drop(drops,axis=1)
# Drops the correlated columns
# for i in drops:
# col=(x.iloc[:, (i+1):(i+2)].columns.values.tolist())
# print (col)
# df_dropped=df.drop(col, axis=1)
#cols.append()
#print(df_dropped)
return (df_dropped)
但这段代码正在打印的数据帧中只删除了一列。对此有何意见或建议?在
提前谢谢