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

不同长度数据集合并:为买家数据集匹配添加卖家性别字段

Add Seller Gender to Buyer Dataset (TYPE 2)

Got it, let's work through this problem step by step to map the seller's genders field from your TYPE 1 dataset to every row in the TYPE 2 buyer dataset.

Key Matching Logic

First, let's confirm the exact keys we need for a precise match:

  • Same period (matches are constrained to the same cycle)
  • Same gp (group, ensuring we pair buyers and sellers within the same group)
  • Buyer's partner = Seller's subject (your data explicitly states the partner field in the buyer dataset corresponds to the seller's subject ID)

This combination guarantees we pull the correct seller gender for each buyer, even when one seller is matched to multiple buyers.


Solution 1: Python (Pandas)

If you're using Python, here's a clean way to merge the datasets:

import pandas as pd

# Load your datasets (replace with your actual file paths or existing data frames)
buyers_df = pd.read_csv("buyers_type2.csv")
sellers_df = pd.read_csv("sellers_type1.csv")

# Merge datasets using our matching keys
merged_buyers = pd.merge(
    buyers_df,
    # Only select necessary seller fields to avoid clutter
    sellers_df[["period", "gp", "subject", "genders"]],
    left_on=["period", "gp", "partner"],
    right_on=["period", "gp", "subject"],
    how="left"  # Keep all buyer rows; use "inner" if you only want fully matched rows
)

# Clean up duplicate columns from the merge
merged_buyers = merged_buyers.drop(columns=["subject_y"])
merged_buyers = merged_buyers.rename(columns={"subject_x": "subject"})

# Inspect the result
print(merged_buyers.head())

Solution 2: R (dplyr)

For R users, the dplyr package makes this task straightforward:

library(dplyr)

# Load your datasets (update file paths as needed)
buyers_df <- read.csv("buyers_type2.csv")
sellers_df <- read.csv("sellers_type1.csv")

# Merge to add seller gender to buyer data
merged_buyers <- buyers_df %>%
  left_join(
    # Select only necessary seller columns
    sellers_df %>% select(period, gp, subject, genders),
    by = c("period" = "period", "gp" = "gp", "partner" = "subject")
  )

# View the first few rows of the result
head(merged_buyers)

Verify the Match

To ensure all matches are accurate (and catch any missing data), check for rows where genders is missing:

  • Python: merged_buyers[merged_buyers["genders"].isna()]
  • R: merged_buyers %>% filter(is.na(genders))

If you see missing values, double-check that period, gp, and partner/subject values align perfectly between the two datasets (no typos or formatting mismatches like integer vs string IDs).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:03:51