首页 > 解决方案 > 如何在没有NaN的情况下从excel中提取指定行

问题描述

为什么使用此代码会出现“raise KeyError(list(np.compress(check, subset)))”?

我想从没有 NaN 的 excel 文件中提取指定的行和列。

readFile = 'testFile'
input_book = pd.ExcelFile(readFile) 
df_list = []

for sheet in input_book.sheet_names:
    df_list.append(input_book.parse(sheet)) 
    for d in df_list:
        print(d.dropna(subset=['test1', 'test2']))

这是数据(每个 | 中的分隔单元格)。索引是我自己创建的列。


Index | test1   | test2 | test3

1     |apple   | stone  | Red

NaN   |banana  | stone  | Blue

NaN   | orange | stone  | Yellow  

  2   |  kiwi  | stone2  | White

NaN   | cake   | stone2  Black

我想这样做。


Index | test1  | test2

1     | apple  | stone  

2     | kiwi   | stone2

标签: pythonpandasopenpyxlxlrd

解决方案


如果Index是列将其添加到列表中:

for d in df_list:
    print(d.dropna(subset=['Index','test1', 'test2', 'test3']))

如果需要从所有列中删除缺失值:

for d in df_list:
    print(d.dropna())

推荐阅读