You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在多列DataFrame中高效匹配行数据获取行索引的方法

高效查找DataFrame中匹配目标行的索引方法

当DataFrame列数较多时,以下几种方法可以高效定位目标行对应的索引:

方法一:布尔匹配(中小数据量首选)

直接将目标行转为与DataFrame列对齐的Series,逐列比较后筛选出所有列都匹配的行,再提取索引。代码简洁,无需额外预处理。

import pandas as pd

df = pd.DataFrame({'X1': ['Falcon', 'Falcon', 'Parrot', 'Parrot'],
                   'X2': ['Captive', 'Wild', 'Captive', 'Wild'],
                   'X3': ['BIG', 'SMALL', 'BIG', 'SMALL'],
                   'X4': [0,1,0,0],
                   'X5': [1,0,0,0]})

target_row = ['Parrot', 'Captive','BIG',0,0]
# 生成与df列匹配的目标Series
target_series = pd.Series(target_row, index=df.columns)
# 筛选所有列均匹配的行
match_mask = df.eq(target_series).all(axis=1)
# 获取匹配的索引列表
result_indices = df.index[match_mask].tolist()

print(result_indices)  # 输出: [2]

方法二:哈希匹配(大数据量单次查询)

将每行数据转换为唯一哈希值,通过对比哈希值快速定位目标行。适合列数多、行数多的场景,比逐列比较更高效。

import pandas as pd
import hashlib

df = pd.DataFrame({'X1': ['Falcon', 'Falcon', 'Parrot', 'Parrot'],
                   'X2': ['Captive', 'Wild', 'Captive', 'Wild'],
                   'X3': ['BIG', 'SMALL', 'BIG', 'SMALL'],
                   'X4': [0,1,0,0],
                   'X5': [1,0,0,0]})

def calc_row_hash(row):
    # 将行数据拼接为字符串后计算MD5哈希
    row_str = '|'.join(str(val) for val in row)
    return hashlib.md5(row_str.encode()).hexdigest()

# 为DataFrame添加哈希列
df['row_hash'] = df.apply(calc_row_hash, axis=1)

target_row = ['Parrot', 'Captive','BIG',0,0]
target_hash = calc_row_hash(target_row)
# 获取匹配的索引
result_indices = df.index[df['row_hash'] == target_hash].tolist()

print(result_indices)  # 输出: [2]
# 用完可删除哈希列
df.drop('row_hash', axis=1, inplace=True)

方法三:哈希映射字典(大数据量多次查询)

预先构建哈希值到行索引的映射字典,后续查询直接通过字典取值,查询时间复杂度为O(1),适合需要多次查找不同目标行的场景。

import pandas as pd
import hashlib

df = pd.DataFrame({'X1': ['Falcon', 'Falcon', 'Parrot', 'Parrot'],
                   'X2': ['Captive', 'Wild', 'Captive', 'Wild'],
                   'X3': ['BIG', 'SMALL', 'BIG', 'SMALL'],
                   'X4': [0,1,0,0],
                   'X5': [1,0,0,0]})

def calc_row_hash(row):
    row_str = '|'.join(str(val) for val in row)
    return hashlib.md5(row_str.encode()).hexdigest()

# 构建哈希值到索引列表的映射(支持重复行)
hash_index_map = {}
for idx, row in df.iterrows():
    row_hash = calc_row_hash(row)
    if row_hash not in hash_index_map:
        hash_index_map[row_hash] = []
    hash_index_map[row_hash].append(idx)

target_row = ['Parrot', 'Captive','BIG',0,0]
target_hash = calc_row_hash(target_row)
# 直接从字典获取匹配索引
result_indices = hash_index_map.get(target_hash, [])

print(result_indices)  # 输出: [2]

适用场景总结

  • 中小规模DataFrame:优先用布尔匹配,代码简洁易维护;
  • 大规模单次查询:用哈希匹配,减少逐列比较的开销;
  • 大规模多次查询:用哈希映射字典,预处理一次后,每次查询都能快速得到结果。

内容的提问来源于stack exchange,提问作者Soon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:05:26