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

跨工作表多列比对与CSV大规模数据地区名称匹配技术问询

Alright, let's break down your two data matching challenges with practical, tool-specific solutions—no fluff, just what works:

需求1:比对两个工作表多列数据并返回目标列

The approach here depends on whether you're using Excel or a database. Let's cover both:

Excel Solution

If you're working in Excel (especially 365/2021), XLOOKUP with multi-condition matching is the cleanest option. Suppose you want to match columns A, B, C from Sheet1 to Sheet2, and pull the corresponding value from Sheet2's column D:

=XLOOKUP(1,(Sheet1!A2=Sheet2!A:A)*(Sheet1!B2=Sheet2!B:B)*(Sheet1!C2=Sheet2!C:C),Sheet2!D:D,"No Match")
  • The (condition1)*(condition2)*(condition3) trick works because Excel treats TRUE as 1 and FALSE as 0—only when all conditions are met does the product equal 1, triggering the match.
  • For older Excel versions (pre-365), use INDEX + MATCH array formula (press Ctrl+Shift+Enter after typing it):
    =INDEX(Sheet2!D:D,MATCH(1,(Sheet1!A2=Sheet2!A:A)*(Sheet1!B2=Sheet2!B:B)*(Sheet1!C2=Sheet2!C:C),0))
    

SQL Solution

If your data is in a database (MySQL, PostgreSQL, SQLite, etc.), use a JOIN to align the tables on multiple columns:

SELECT 
    t1.*, 
    t2.target_column  -- Replace with your actual target column name
FROM table1 t1
LEFT JOIN table2 t2
    ON t1.column_a = t2.column_a
    AND t1.column_b = t2.column_b
    AND t1.column_c = t2.column_c;
  • Use LEFT JOIN to keep all rows from table1 even if there's no match in table2 (mismatched rows will show NULL for the target column). Switch to INNER JOIN if you only want rows that have matches in both tables.

需求2:批量匹配300万行CSV数据的地区名称

3 million rows is too large for standard Excel formulas (they'll lag or crash), so we'll focus on scalable tools like SQL or Python, plus a Power Query workaround for Excel users.

SQL Solution (SQLite, no server required)

SQLite is perfect for local CSV processing—no setup needed. Here's how:

  1. Import all 7 CSV files and your region mapping file into SQLite (use tools like DB Browser for SQLite for a GUI, or command-line).
  2. Run this query to match region names:
SELECT 
    cd.country_code,
    rm.region_name,
    cd.city_name  -- Add other columns from your city data as needed
FROM city_data cd
LEFT JOIN region_mapping rm
    ON cd.country_code = rm.country_code
    AND cd.region_id = rm.region_id;
  1. Export the result as a CSV directly from the tool (or use SQLite commands: .mode csv followed by .output matched_cities.csv).

Python Solution (Pandas, efficient for big data)

Pandas handles large datasets smoothly, even with 3 million rows. Here's a script:

import pandas as pd

# Load the region mapping file
mapping_df = pd.read_csv("region_mapping.csv")

# Load all 7 city CSV files and combine them
city_dfs = []
for file_num in range(1, 8):
    file_path = f"city_data_{file_num}.csv"  # Adjust your file naming pattern
    df = pd.read_csv(file_path)
    city_dfs.append(df)
combined_city_df = pd.concat(city_dfs, ignore_index=True)

# Match region names using multi-column join
matched_df = pd.merge(
    combined_city_df,
    mapping_df,
    on=["country_code", "region_id"],
    how="left"  # Keeps all city rows, even if no region match is found
)

# Export the final matched data to CSV
matched_df.to_csv("matched_city_data.csv", index=False)
  • If you hit memory limits, use chunksize=100000 in pd.read_csv() to process files in smaller batches.

Excel Power Query Workaround

If you must use Excel, Power Query is the only feasible way (it's designed for big data):

  1. Go to Data > Get Data > From File > From Folder and select the folder with your 7 CSV files.
  2. Use the "Combine & Load" option to merge all CSV files into a single table.
  3. Import your region mapping file via Data > Get Data > From File > From CSV.
  4. In the Power Query Editor, go to Home > Merge Queries > Merge Queries as New.
  5. Select your combined city table and mapping table, then choose country_code and region_id as the matching columns.
  6. Expand the merged column to pull in the region_name, then load the result back to Excel or export as a CSV.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:29:32