python - 如何在没有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
解决方案
如果Index
是列将其添加到列表中:
for d in df_list:
print(d.dropna(subset=['Index','test1', 'test2', 'test3']))
如果需要从所有列中删除缺失值:
for d in df_list:
print(d.dropna())
推荐阅读
- swiftui - 删除第一个字符时,SwiftUI TextField 冻结
- python - Selenium 在使用 Python 通过类名查找不存在时很慢
- python - 将python文本游戏放入html的最简单方法?
- python - 蟒蛇中的曼德布洛特
- python - 如何并行化“for”循环?
- python - 基于端口(8001、8002、8003)的本地主机上的Django站点框架
- scala - 用于主题标签的 Scala 正则表达式
- mysql - SELECT 任职时间最长的分行经理
- continuous-integration - 是否可以通过 Slack 消息触发 TravisCI 构建而无需启动我自己的服务器?
- foreach - ForEach 通过多个 TextField 来验证 SwiftUI 中是否为空