如何在CPW Detections表多列物种字段中查找目标物种相关行
查找CPW Detections表格中指定物种的完整行
Excel公式实现
如果用Excel处理,可借助FILTER函数快速筛选:
- 把要查找的物种放在任意单元格(比如
Z1),输入以下公式提取匹配行:
注:=FILTER('CPW Detections'!A:U, ('CPW Detections'!E:E=Z1)+('CPW Detections'!G:G=Z1)+...+('CPW Detections'!AI:AI=Z1), "无匹配结果")E:E对应Species 1列,G:G对应Species 2列,依次类推到AI:AI(Species 21列),加号代表逻辑或,只要任意Species列匹配目标物种,就返回整行数据。 - 对应检测数可直接查看该行的对应Detection列(比如Species 1对应Detection 1列)。
Python Pandas实现
用Python分析的话,步骤如下:
- 读取表格数据:
import pandas as pd df = pd.read_excel("CPW Detections.xlsx") # 若为CSV文件改用read_csv - 筛选包含目标物种的行:
target_species = "Numbat" # 可替换为任意物种 # 提取所有Species开头的列 species_cols = [col for col in df.columns if col.startswith("Species")] # 筛选任意Species列匹配目标物种的行 filtered_df = df[df[species_cols].eq(target_species).any(axis=1)] - 提取对应检测数:
运行后def get_detection(row): for i in range(1,22): if row[f"Species {i}"] == target_species: return row[f"Detection {i}"] filtered_df["目标检测数"] = filtered_df.apply(get_detection, axis=1)filtered_df就是包含目标物种的完整行,且新增了对应检测数的列。
SQL查询实现
如果表格存储在数据库中,用以下语句查询:
SELECT * FROM `CPW Detections` WHERE `Species 1` = 'Numbat' OR `Species 2` = 'Numbat' ... OR `Species 21` = 'Numbat';
若要同时获取对应检测数,可添加CASE语句:
SELECT *, CASE WHEN `Species 1` = 'Numbat' THEN `Detection 1` WHEN `Species 2` = 'Numbat' THEN `Detection 2` ... WHEN `Species 21` = 'Numbat' THEN `Detection 21` END AS 目标检测数 FROM `CPW Detections` WHERE `Species 1` = 'Numbat' OR `Species 2` = 'Numbat' ... OR `Species 21` = 'Numbat';
内容的提问来源于stack exchange,提问作者Madison Read
相关产品推荐
相关产品推荐

