You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法从Pandas DataFrame删除对应最小权重值列的问题求助

问题与解决方法

问题场景

我有一个名为df_col的DataFrame,数据如下:

print(df_col)
    c1  c2  c3  c4  c5  c6  c7  c8  c9  c10  c11  c12  c13  c14  c15  c16  \
0    5   4   4   5   4   4   4   4   4    5    4    4    3    3    4    3   
1    3   4   3   4   4   5   3   5   3    3    5    3    3    2    3    3   
2    4   4   5   5   4   4   4   4   4    4    5    4    4    1    3    2   
3    5   5   5   5   1   5   5   5   5    1    5    4    5    5    5    4   
4    5   5   5   5   5   5   5   4   5    2    5    4    4    5    2    2   
74   4   4   4   4   5   5   5   5   5    4    5    5    4    2    5    4   
75   5   3   4   5   5   5   4   5   4    5    5    4    4    4    4    3   
76   5   3   3   5   2   3   3   3   3    3    3    3    5    5    3    3   
77   4   5   4   2   2   4   4   4   5    4    5    5    3    3    4    2   
78   5   4   5   5   5   5   4   5   5    5    5    5    4    2    5    3   

    c17  c18  c19  c20  
0     4    4    3    3  
1     3    5    5    1  
2     3    3    4    3  
3     5    4    5    5  
4     5    5    5    3  
74    4    5    5    4  
75    3    4    5    4  
76    5    3    5    3  
77    4    5    5    2  
78    4    5    5    4  

同时有权重列表each_c:

each_c = [0.062, 0.031, 0.015, -0.016, 0.015, -0.031, 0.0, 0.015, 0.109, 0.0, 0.0, -0.031, -0.016, 0.219, 0.172, 0.031, 0.109, -0.016, 0.0, 0.062]

我需要删除each_c中最小值(-0.031)对应的列(c6和c12),但以下代码无法实现需求:

df_eachc = pd.DataFrame(each_c).T 
df_col.append(df_eachc, ignore_index=True)   
minui = min(each_c)  
for k in df_col:
   if minui < 0 :
      if k == minui:
        df_col.drop([k], axis=1, inplace=True, errors='raise')
        df_col.drop([minui], axis=1, inplace=True, errors='raise')

原代码问题

  1. df_col.append()返回新的DataFrame,不会修改原数据,必须重新赋值才生效
  2. 遍历df_col时,k是列名(如c1),和数值类型的minui(-0.031)永远不相等,删除逻辑完全错误
  3. 无需将权重添加到DataFrame,直接通过权重和列的对应关系就能定位目标列

正确解决方案

方法1:直接删除目标列(无需添加权重行)

import pandas as pd

# 定义权重列表
each_c = [0.062, 0.031, 0.015, -0.016, 0.015, -0.031, 0.0, 0.015, 0.109, 0.0, 0.0, -0.031, -0.016, 0.219, 0.172, 0.031, 0.109, -0.016, 0.0, 0.062]

# 找到权重最小值
min_weight = min(each_c)

# 获取所有权重等于最小值的列索引
target_indices = [i for i, w in enumerate(each_c) if w == min_weight]

# 转换为对应列名(c1到c20,索引从0开始)
cols_to_drop = [f'c{idx+1}' for idx in target_indices]

# 删除列
df_col.drop(cols_to_drop, axis=1, inplace=True)

# 查看结果
print(df_col)

方法2:先添加权重行再删除列

如果确实需要将权重列表作为一行添加到DataFrame末尾,再删除目标列:

# 添加权重行到DataFrame
df_eachc = pd.DataFrame([each_c], columns=df_col.columns)
df_col = pd.concat([df_col, df_eachc], ignore_index=True)

# 执行删除操作(和方法1逻辑一致)
min_weight = min(each_c)
target_indices = [i for i, w in enumerate(each_c) if w == min_weight]
cols_to_drop = [f'c{idx+1}' for idx in target_indices]
df_col.drop(cols_to_drop, axis=1, inplace=True)

内容的提问来源于stack exchange,提问作者user18737194

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 04:35:28