csv 如何使用dataframe.drop()从列中删除few标签

smtd7mpg  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(99)

df = pd.read_csv("idx.csv")用于读取数据;以下是数据

activity   time_s   lw_x   lw_y  ...   la_z   ra_x   ra_y   ra_z
72920          2   729.21 -1.273 -0.465  ...  0.094  1.555  0.676  0.543
72921          2   729.22 -1.430 -0.488  ...  0.094  3.078  0.977  1.996
72922          2   729.23 -1.559 -0.465  ...  0.078  3.445  2.590  2.266
72923          2   729.24 -1.609 -0.422  ...  0.066  0.066  2.652 -0.102
72924          2   729.25 -1.594 -0.352  ...  0.059 -1.723  0.945 -1.156
74552          3   745.53 -1.176 -0.828  ...  0.090 -0.082  1.223  0.148
74553          3   745.54 -1.176 -0.863  ...  0.090 -0.090  1.242  0.137
74554          3   745.55 -1.250 -0.813  ...  0.086 -0.086  1.246  0.105
74555          3   745.56 -1.332 -0.797  ...  0.094 -0.074  1.246  0.059
74556          3   745.57 -1.383 -0.777  ...  0.094 -0.066  1.238  0.023
82045          1   820.46 -0.512 -1.320  ...  0.145 -0.574  0.570 -0.031
82046          1   820.47 -0.563 -1.352  ...  0.102 -0.527  0.715 -0.004
82047          1   820.48 -0.617 -1.359  ...  0.063 -0.496  0.930  0.195
82048          1   820.49 -0.660 -1.352  ...  0.066 -0.469  1.051  0.242
82049          1   820.50 -0.711 -1.336  ...  0.055 -0.313  1.215  0.344
82205         77   822.06  0.641 -1.434  ...  0.082 -0.168  0.996  0.102
82206         77   822.07  0.887 -1.434  ...  0.078 -0.168  0.996  0.102
82207         77   822.08  1.027 -1.383  ...  0.078 -0.168  1.000  0.102
82208         77   822.09  1.156 -1.246  ...  0.078 -0.168  1.004  0.109
82209         77   822.10  1.527 -1.012  ...  0.078 -0.172  1.008  0.117
100795        99  1007.96  0.000  0.000  ... -0.980  0.684  0.242 -0.734
100796        99  1007.97  0.000  0.000  ... -1.008  0.684  0.234 -0.738
100797        99  1007.98  0.000  0.000  ... -0.996  0.684  0.230 -0.730
100798        99  1007.99  0.000  0.000  ... -0.996  0.688  0.227 -0.730
100799        99  1008.00  0.000  0.000  ... -0.984  0.684  0.227 -0.734

我尽力了

df2 = df.drop(axis=0, index=["77","99"])

但显示以下错误

File ~\anaconda3\lib\site-packages\pandas\core\indexes\base.py:6644 in drop
    raise KeyError(f"{list(labels[mask])} not found in axis")

KeyError: "['77', '99'] not found in axis"

如果可能的话,请在您方便的时候提出一个相关的解决方案,从活动栏中删除77,99标签。Thanks!

ifsvaxew

ifsvaxew1#

77和99不是指数的一部分,而是“活动”一栏的一部分。首先,你需要解析索引:

>>> df.drop(df.index[df['activity'].isin([77, 99])])
       activity  time_s   lw_x   lw_y   la_z   ra_x   ra_y   ra_z
72920         2  729.21 -1.273 -0.465  0.094  1.555  0.676  0.543
72921         2  729.22 -1.430 -0.488  0.094  3.078  0.977  1.996
72922         2  729.23 -1.559 -0.465  0.078  3.445  2.590  2.266
72923         2  729.24 -1.609 -0.422  0.066  0.066  2.652 -0.102
72924         2  729.25 -1.594 -0.352  0.059 -1.723  0.945 -1.156
74552         3  745.53 -1.176 -0.828  0.090 -0.082  1.223  0.148
74553         3  745.54 -1.176 -0.863  0.090 -0.090  1.242  0.137
74554         3  745.55 -1.250 -0.813  0.086 -0.086  1.246  0.105
74555         3  745.56 -1.332 -0.797  0.094 -0.074  1.246  0.059
74556         3  745.57 -1.383 -0.777  0.094 -0.066  1.238  0.023
82045         1  820.46 -0.512 -1.320  0.145 -0.574  0.570 -0.031
82046         1  820.47 -0.563 -1.352  0.102 -0.527  0.715 -0.004
82047         1  820.48 -0.617 -1.359  0.063 -0.496  0.930  0.195
82048         1  820.49 -0.660 -1.352  0.066 -0.469  1.051  0.242
82049         1  820.50 -0.711 -1.336  0.055 -0.313  1.215  0.344

不带drop的快捷方式:

# Note the ~ to inverse the boolean mask True -> False and False -> True
>>> df[~df['activity'].isin([77, 99])]
       activity  time_s   lw_x   lw_y   la_z   ra_x   ra_y   ra_z
72920         2  729.21 -1.273 -0.465  0.094  1.555  0.676  0.543
72921         2  729.22 -1.430 -0.488  0.094  3.078  0.977  1.996
72922         2  729.23 -1.559 -0.465  0.078  3.445  2.590  2.266
72923         2  729.24 -1.609 -0.422  0.066  0.066  2.652 -0.102
72924         2  729.25 -1.594 -0.352  0.059 -1.723  0.945 -1.156
74552         3  745.53 -1.176 -0.828  0.090 -0.082  1.223  0.148
74553         3  745.54 -1.176 -0.863  0.090 -0.090  1.242  0.137
74554         3  745.55 -1.250 -0.813  0.086 -0.086  1.246  0.105
74555         3  745.56 -1.332 -0.797  0.094 -0.074  1.246  0.059
74556         3  745.57 -1.383 -0.777  0.094 -0.066  1.238  0.023
82045         1  820.46 -0.512 -1.320  0.145 -0.574  0.570 -0.031
82046         1  820.47 -0.563 -1.352  0.102 -0.527  0.715 -0.004
82047         1  820.48 -0.617 -1.359  0.063 -0.496  0.930  0.195
82048         1  820.49 -0.660 -1.352  0.066 -0.469  1.051  0.242
82049         1  820.50 -0.711 -1.336  0.055 -0.313  1.215  0.344

相关问题