Python Dataframe 列匹配

发布于 2025-01-10 18:44:07 字数 367 浏览 0 评论 0原文

我有 2 列 A & B. A 包含由“_”分隔的文本值,B 有一些与 A 相关的描述。

示例:

*Col         Terms
AB_BCN_PRC   About Bitcoin Price
AC_CR_STT    Account credit Statement
A6_AT_MD     Audi Automatic Model*

我需要映射 A 和 B,因此将来如果 A 中出现新值,它应该自动构建 B

示例:

*A         B
AB_CR_STT  About credit Statement*

任何想法或可能如何完成这件事?

I have 2 columns A & B. A contains text values separated by '_' and B has some description related to A.

Example:

*Col         Terms
AB_BCN_PRC   About Bitcoin Price
AC_CR_STT    Account credit Statement
A6_AT_MD     Audi Automatic Model*

I need to map A and B so in future if a new value comes in A it should frame B automatically

Example:

*A         B
AB_CR_STT  About credit Statement*

Any idea or possible ways to get this done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紅太極 2025-01-17 18:44:07

如果你能定义一个好的完整的映射字典(每个名字用_分隔作为键,这个名字代表的文本作为值),就可以很容易实现,最后我用pandas组合成你例子中的格式,也可以直接使用print,主要关注函数gen_terms,通过你提供的名称返回全文,你可以在此基础上根据你需要的功能进行扩展(确保你传入的名称中的每一项,由 _ 分隔,是您定义的在col_map中):

import pandas as pd

def gen_terms(col):
    col_map = {
        "AB": "About",
        "BCN": "Bitcoin",
        "PRC": "Price",
        "AC": "Account",
        "CR": "credit",
        "STT": "Statement",
        "A6": "Audi",
        "AT": "Automatic",
        "MD": "Model",
    }
    spans = col.split("_")
    return " ".join([col_map.get(data) for data in spans])


name = "AB_CR_STT"

df = pd.DataFrame({"A": ["AB_CR_STT"], "B": [gen_terms("AB_CR_STT")]})

print(df)

#            A                       B
# 0  AB_CR_STT  About credit Statement

If you can define a good complete mapping dictionary (each name separated by _ as a key, the text represented by this name as a value), it can be easily implemented, and finally I used pandas to combine into the format in your example, you can also use print directly, the main focus on the function gen_terms, by the name you provide to return the full text, you can expand on this basis according to the functions you need(Make sure that each item in the name you pass in, separated by _, is the one you defined in the col_map):

import pandas as pd

def gen_terms(col):
    col_map = {
        "AB": "About",
        "BCN": "Bitcoin",
        "PRC": "Price",
        "AC": "Account",
        "CR": "credit",
        "STT": "Statement",
        "A6": "Audi",
        "AT": "Automatic",
        "MD": "Model",
    }
    spans = col.split("_")
    return " ".join([col_map.get(data) for data in spans])


name = "AB_CR_STT"

df = pd.DataFrame({"A": ["AB_CR_STT"], "B": [gen_terms("AB_CR_STT")]})

print(df)

#            A                       B
# 0  AB_CR_STT  About credit Statement
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文