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

基于Pandas处理Excel重复数据:替换自定义去重函数的疑问

Hey there! Let's work through your Pandas issues step by step—since you're a beginner, I'll keep things clear and actionable. First, let's break down what's likely going wrong with your (1)(2) code snippets for both tasks, then fix them.

Task 1: Group by LicNo & Split Licensee into Multiple Columns

Common Mistakes in Your (1)(2) Code

Chances are your original code is either merging Licensee values into a single string (instead of separate columns) or failing to properly expand grouped lists into columns. For example, maybe your code looks like this:

# 错误的(1)(2)代码示例
(1) grouped_data = df.groupby('LicNo')['Licensee'].agg(lambda x: ', '.join(x))
(2) result_df = grouped_data.reset_index()

This gives you one column of combined Licensee strings, not multiple distinct Licensee columns. Or if you used apply(list) but didn't split the list into columns:

# 另一种错误示例
(1) grouped_data = df.groupby('LicNo')['Licensee'].apply(list)
(2) result_df = pd.DataFrame(grouped_data)

This leaves you with a single column of lists, which isn't what you need.

Fixed Code for Task 1

We need to group the Licensee values into lists, then expand those lists into separate columns, and reattach the LicNo column:

(1) # 分组并将同LicNo的Licensee转为列表,同时重置索引保留LicNo列
    grouped = df.groupby('LicNo')['Licensee'].apply(list).reset_index()
(2) # 将列表拆分为多列,添加前缀区分,再和LicNo列合并
    final_task1 = grouped['Licensee'].apply(pd.Series).add_prefix('Licensee_').join(grouped['LicNo'])
  • Line (1): Groups by LicNo and collects all matching Licensee values into a list, then resets the index so LicNo becomes a regular column instead of the index.
  • Line (2): Uses apply(pd.Series) to split each list into its own column, adds a prefix like Licensee_0, Licensee_1 to keep columns distinct, then joins back with the LicNo column.

Task 2: Check if First Licensee's First Word Appears in Subsequent Columns

Common Mistakes in Your (1)(2) Code

Most likely, your code is trying to compare an entire column of first words against all rows at once (instead of checking row-by-row), or not handling missing values, or using the wrong axis in apply. For example:

# 错误的(1)(2)代码示例
(1) first_words = df['Licensee_0'].str.split().str[0]
(2) df['is_duplicate'] = df.iloc[:,1:].apply(lambda x: first_words in x.values)

Here, first_words is a full Series (all rows' first words), not the single first word for the current row. This causes a broadcasting error because you're comparing a Series to individual row values.

Fixed Code for Task 2

We need to process each row individually, grab the first word from the first Licensee column, then check if that word exists in any of the subsequent columns for that row:

(1) # 定义逐行检查的逻辑(可以内嵌,但分开更清晰)
    def check_duplicate_row(row):
        # 处理空值:如果首列是空,直接返回False
        if pd.isna(row.iloc[0]):
            return False
        # 提取首列的第一个单词
        first_word = row.iloc[0].split()[0]
        # 遍历后续所有列,检查是否有包含该单词的非空值
        return any(first_word in str(val) for val in row.iloc[1:] if not pd.isna(val))
(2) # 按行应用检查函数,生成标记列
    final_task1['is_duplicate'] = final_task1.apply(check_duplicate_row, axis=1)
  • Line (1): The helper function handles edge cases (like empty values in the first Licensee column) and checks each subsequent column for the first word. Using str(val) ensures we can handle non-string values (if any) without errors.
  • Line (2): Uses apply with axis=1 to run the function on every row (instead of every column), which is what we need for row-level checks.

Quick Notes for Beginners

  • Always test small subsets of your data first! It's easier to spot errors with 5-10 rows instead of thousands.
  • Use print() or display() to check intermediate steps (like what grouped looks like after line 1) to confirm it's doing what you expect.
  • When working with strings, always account for missing values (pd.isna()) to avoid AttributeError or IndexError.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 20:24:08