我想在pandas中删除名为“1”的列

0 投票
1 回答
91 浏览
提问于 2025-04-14 16:26

我想在pandas中删除一个名为“1”的列,但我遇到了一个keyerror错误。

Traceback (most recent call last):
  File "/usr/lib64/python3.11/tkinter/__init__.py", line 1967, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "/home/tamil/RnD/PreProcessing-1.0.1/app/app.py", line 86, in <lambda>
    del_column_button = tk.Button(preprocessing_frame,text="Delete The Column",command=lambda:open_delete_column_window())
                                                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tamil/RnD/PreProcessing-1.0.1/app/app.py", line 113, in open_delete_column_window
    df = df.drop(column_name,axis = 1)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tamil/.local/lib/python3.11/site-packages/pandas/core/frame.py", line 5568, in drop
    return super().drop(
           ^^^^^^^^^^^^^
  File "/home/tamil/.local/lib/python3.11/site-packages/pandas/core/generic.py", line 4782, in drop
    obj = obj._drop_axis(labels, axis, level=level, errors=errors)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tamil/.local/lib/python3.11/site-packages/pandas/core/generic.py", line 4824, in _drop_axis
    new_axis = axis.drop(labels, errors=errors)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tamil/.local/lib/python3.11/site-packages/pandas/core/indexes/base.py", line 7069, in drop
    raise KeyError(f"{labels[mask].tolist()} not found in axis")
KeyError: "['1'] not found in axis"

这是我的数据框。

     Index                       Test               Result                               Method  Method_new    0    1    2    3
0       0                  WBC COUNT          (H) 70.0 fL                            Impedance           3  0.0  0.0  0.0  1.0
1       1                  RBC COUNT   (H) 31.3 x 10^6/µL                            Impedance           3  0.0  0.0  0.0  1.0
2       2                        MCH          (H) 21.9 pg                           Calculated           1  0.0  1.0  0.0  0.0
3       3                       MCHC        (H) 30.1 g/dL                           Calculated           1  0.0  1.0  0.0  0.0
4       4                        MCV          (H) 72.3 fL                            Impedance           3  0.0  0.0  0.0  1.0
5       5             PLATELET COUNT  (H) 167.0 x 10^3/µL                            Impedance           3  0.0  0.0  0.0  1.0
6       6                        RDW           (H) 11.8 %                           Calculated           1  0.0  1.0  0.0  0.0
7       7                        MPV         (H) 0.136 fL                            Impedance           3  0.0  0.0  0.0  1.0
8       8                        PCT            (H) 0.9 %                           Calculated           1  0.0  1.0  0.0  0.0
9       9                  TOTAL WBC    (H) 8.2 x 10^3/µL  Automated (Sysmex/light microscopy)           0  1.0  0.0  0.0  0.0
10     10                 NEUTROPHIL           (H) 65.9 %                   Differential count           2  0.0  0.0  1.0  0.0
11     11                 LYMPHOCYTE           (H) 18.4 %                   Differential count           2  0.0  0.0  1.0  0.0
12     12                 EOSINOPHIL            (H) 9.0 %                   Differential count           2  0.0  0.0  1.0  0.0
13     13                   MONOCYTE            (H) 6.5 %                   Differential count           2  0.0  0.0  1.0  0.0
14     14                   BASOPHIL            (H) 0.0 %                   Differential count           2  0.0  0.0  1.0  0.0
15     15  ABSOLUTE NEUTROPHIL COUNT   (H) 53.8 x 10^3/µL                           Calculated           1  0.0  1.0  0.0  0.0
16     16  ABSOLUTE EOSINOPHIL COUNT    (H) 1.0 x 10^3/µL                           Calculated           1  0.0  1.0  0.0  0.0
17     17  ABSOLUTE LYMPHOCYTE COUNT   (H) 12.9 x 10^3/µL                           Calculated           1  0.0  1.0  0.0  0.0

但是当我尝试这样做时:

df  = df.drop(column_name,axis = 1) 

->列名是“1”

我得到了上面的错误。

找到了解决方案:

我把所有的列名转换成了一个列表,然后把列表中的所有元素都变成了字符串,找到了'1'的索引,并用它来删除那一列。

column_name = '1'
list_string = df.columns.tolist()
list_column_names = [str(x) for x in list_string]
column_index  = list_column_names.index(column_name)
df = df.drop(df.columns[column_index], axis=1)

我觉得在读取值的时候可能有问题,因为drop()函数可能把1和'1'当成不同的东西,不过我不太确定。

如果有人能找到更好的解决方案,请告诉我 :)

顺便说一下,我正在使用pandas和python 3.11。

1 个回答

1

这个错误提示说的是 KeyError: "['1'] not found in axis",意思是你引用的列不对,或者说 '1' 这个列根本就不存在于数据表里。

要查看你数据表里的列,可以用下面的命令:

df.columns

或者你也可以用:

print(df.columns)

如果你想删除一列(或者说“去掉”),可以这样做:

df.drop(['column_name1', 'column_name2'], axis=1, inplace=True)
  • 你要去掉的列名 'column_name' 可以放在一个列表里,这样你可以一次去掉多个列。
  • axis=1 表示你想去掉的是列(如果是 axis=0 就是按行去掉),
  • inplace=True 意思是说这个改变会直接应用到数据表上,而不需要再把结果赋值给一个新变量。

撰写回答