Skip to main content
 首页 » 编程设计

python之如何索引CSV文件以高效搜索

2025年05月04日14dflying

我有一个包含超过 40 万行和大约 20 列的 CSV 文件,因此当我查找特定值时,搜索 CSV 文件需要花费太多时间。我想知道如何映射文件,以便高效快速地搜索某个值。

提前致谢!我很乐意回答任何问题!

请您参考如下方法:

假设您可以将所有内容存储在 RAM 中(即加载它),我会使用 pandas。

import pandas as pd 
csv_loc = 'C:/yourcsv.csv' 
df = pd.read_csv(csv_loc, sep=',', header=0) #http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html 
df['name'] # would select a column called name 
# This would show observations which start with STARBUC 
match = df['name'].str.contains('(^STARBUC)') 
print(df['name'][match].value_counts()) 

等等。 This is a great tutorial and this