我的目标是:我希望删除在特定列中具有NaN的行。我将允许NaN存在于某些列上,但不允许在其他列中存在。
中文示例:若一行中的'detail\u 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))
哪些输出:
^{pr2}$
当我试图删除列值为NaN的行时,以下内容:df.dropna(subset=[2,3,4,5,6,7,8,9,11,12,13,14], axis=1, inplace=True, how='any')
我得到以下错误:Traceback (most recent call last):
File "clean.py", line 10, in
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]
这很奇怪,因为这很有效:df.dropna(subset=[2], axis=1, inplace=True, how='any')
但不是这个:df.dropna(subset=[5], axis=1, inplace=True, how='any')
所以某些列或这些列中的值一定有问题。下面是我的数据测向头(3) 公司名称: