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

如何将列表映射到account type列并生成含cc/non cc的新列

问题解决:根据指定列表标记账户类型

现有代码列表 code = ['1', '2', '3'],需将其映射至包含数值1、2、3等的account type列,规则为:若account type的值存在于该列表中,新列对应值设为cc;否则设为non cc。以下是几种常用实现方案:

Pandas(Python)实现

方法1:高效的isin + np.where

先统一数据类型(code是字符串,account type是数值,需转字符串匹配),再用矢量化操作生成新列,性能最优:

import pandas as pd
import numpy as np

# 示例数据
df = pd.DataFrame({'account type': [1, 2, 4, 3, 5]})
code = ['1', '2', '3']

# 一步生成新列
df['account_tag'] = np.where(df['account type'].astype(str).isin(code), 'cc', 'non cc')

方法2:apply自定义逻辑

适合小数据量,逻辑直观:

df['account_tag'] = df['account type'].apply(lambda x: 'cc' if str(x) in code else 'non cc')

SQL 实现

如果在数据库中处理,使用CASE语句实现逻辑:

SELECT 
    `account type`,
    CASE 
        WHEN CAST(`account type` AS CHAR) IN ('1', '2', '3') THEN 'cc'
        ELSE 'non cc'
    END AS account_tag
FROM your_table;

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:33:07