Python - Pandas - 对特定子集的 dropna 调用期间出现关键错误

Rya*_*yan 2 python dataframe pandas

我的目标:我希望删除特定列中具有 NaN 的行。我将允许 NaN 存在于某些列上,但不允许存在于其他列上。英文示例:如果一行中“detail_age”的值为 NaN,我想删除该行。

这是我的数据的视图:

import pandas as pd
df = pd.read_csv('allDeaths.csv', index_col=0, nrows=3, engine='python')
print(df.shape)
print(list(df))
Run Code Online (Sandbox Code Playgroud)

哪个输出:

(3,15)
['education_1989_revision', 'education_2003_revision', 
'education_reporting_flag', 'sex', 'detail_age', 'marital_status', 
'current_data_year', 'injury_at_work', 'manner_of_death', 'activity_code', 
'place_of_injury_for_causes_w00_y34_except_y06_and_y07_', '358_cause_recode', 
'113_cause_recode', '39_cause_recode', 'race']
Run Code Online (Sandbox Code Playgroud)

当我尝试删除列值为 NaN 的行时,如下所示:

df.dropna(subset=[2,3,4,5,6,7,8,9,11,12,13,14], axis=1, inplace=True, how='any')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Traceback (most recent call last):
  File "clean.py", line 10, in <module>
    df.dropna(subset=[2,3,4,5,6,7,8,9,11,12,13,14], axis=1, inplace=True, how='any')
  File "/usr/local/lib/python3.4/dist-packages/pandas/core/frame.py", line 3052, in dropna
    raise KeyError(list(np.compress(check, subset)))
KeyError: [3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14]
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为这是有效的:

df.dropna(subset=[2], axis=1, inplace=True, how='any')
Run Code Online (Sandbox Code Playgroud)

但不是这个:

df.dropna(subset=[5], axis=1, inplace=True, how='any')
Run Code Online (Sandbox Code Playgroud)

因此,某些列或这些列中的值一定有问题。这是我使用 df.head(3) 的数据:

作为图像,因为格式很烦人

Max*_*axU 6

演示:

In [360]: df
Out[360]:
      A     B     C   D
0   1.0   2.0   NaN   4
1   5.0   NaN   7.0   8
2   NaN  10.0  11.0  12
3  13.0  14.0  15.0  16

In [362]: df = df.dropna(subset=df.columns[[1,2]], how='any')

In [363]: df
Out[363]:
      A     B     C   D
2   NaN  10.0  11.0  12
3  13.0  14.0  15.0  16
Run Code Online (Sandbox Code Playgroud)

PS当然你可以指定列名:

In [370]: df.dropna(subset=['B','C'], how='any')
Out[370]:
      A     B     C   D
2   NaN  10.0  11.0  12
3  13.0  14.0  15.0  16
Run Code Online (Sandbox Code Playgroud)