为什么我使用此代码会收到“raise KeyError(list(np.compress(check,subset)))”?
我想从 Excel 文件中提取指定的行和列,而不使用 NaN。
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
最佳答案
如果Index
是列,将其添加到列表中:
for d in df_list:
print(d.dropna(subset=['Index','test1', 'test2', 'test3']))
如果需要从所有列中删除缺失值:
for d in df_list:
print(d.dropna())
关于python - 如何从Excel中提取指定行而不使用NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54422869/