python - 从 python 中的数据框中删除多列

标签 python python-3.x pandas dataframe multiple-columns

我想使用 python 从数据框中删除多列(大约 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)

但是这段代码打印的数据帧只删除了一列。对此有何意见或建议?

提前致谢

最佳答案

按数字索引删除多列,如下所示:

cols = [1069, 1068, 1067]

df = df.drop(df.columns[cols], axis=1)

关于python - 从 python 中的数据框中删除多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48629568/

相关文章:

Python file.write() 尝试两次?

python - Python中多重处理后的后处理结果

python - 相比之下,是否超出了最大递归深度?

python - 从 Json 字符串创建 Python 对象

python - 是否可以在 Python 列表理解中编写多个语句?

python - Pandas 以最高值(value)保持复制

python - 动画等高线和散点图

python - 如何旋转长方体?

python - 将字典映射到数据框中的部分字符串匹配

python-3.x - pandas 数据帧中的跳转点 : the moment when the value in a column gets changed