在名为difference的df中有一个名为surface-wordings的列,它的值如下所示:


['paths-modified-/clusters/{cluster_id}/hosts/{host_id}/instructions-operations-modified-POST-parameters-modified-body-reply-schema-properties-modified-step_type-enum-deleted'] 

我定义了一个关键字到标签的映射,我想从这个列中提取关键字并将标签分配给一个名为labels的新列.映射如下:

keyword_label_mappings = {
    'POST-parameters-modified': 'POST Parameters Modified',
    'PUT-parameters-modified': 'PUT Parameters Modified',
    'POST-responses-modified': 'POST Responses Modified',
    'DELETE-summary-from': 'DELETE Summary Changed',
    'POST-responses-deleted': 'POST Responses Deleted',
    'POST-parameters-added': 'POST Parameters Added',
    'POST-parameters-deleted': 'POST Parameters Deleted',
}

我不知道我如何才能做到这一点,任何建议或 idea 都会非常感谢.

编辑:发布print(difference['surface_wordings'].head(5))的输出以供参考:

    surface_wordings
63662   ['openAPI-from', 'openAPI-to', 'paths-added', 'paths-deleted', 'endpoints-added', 'endpoints-deleted', 'servers-deleted', 'components-schemas-deleted']
63661   ['info-title-from', 'info-title-to', 'info-license-deleted', 'info-version-from', 'info-version-to', 'paths-modified-/pets-operations-modified-GET-summary-from', 'paths-modified-/pets-operations-modified-GET-summary-to', 'endpoints-modified-{ method: GET, path: /pets }-summary-from', 'endpoints-modified-{ method: GET, path: /pets }-summary-to']
63659   ['paths-modified-/pets-operations-modified-GET-summary-from', 'paths-modified-/pets-operations-modified-GET-summary-to', 'endpoints-modified-{ method: GET, path: /pets }-summary-from', 'endpoints-modified-{ method: GET, path: /pets }-summary-to']
63658   ['info-version-from', 'info-version-to', 'paths-modified-/pets-operations-modified-GET-summary-from', 'paths-modified-/pets-operations-modified-GET-summary-to', 'endpoints-modified-{ method: GET, path: /pets }-summary-from', 'endpoints-modified-{ method: GET, path: /pets }-summary-to']
63657   ['paths-modified-/pets-operations-modified-GET-summary-from', 'paths-modified-/pets-operations-modified-GET-summary-to', 'endpoints-modified-{ method: GET, path: /pets }-summary-from', 'endpoints-modified-{ method: GET, path: /pets }-summary-to']

推荐答案

我略微修改了你的keyword_label_mappings个词典,让你的第二个样本有一个输出:

keyword_label_mappings = {
    'POST-parameters-modified': 'POST Parameters Modified',
    'PUT-parameters-modified': 'PUT Parameters Modified',
    'POST-responses-modified': 'POST Responses Modified',
    'DELETE-summary-from': 'DELETE Summary Changed',
    'POST-responses-deleted': 'POST Responses Deleted',
    'POST-parameters-added': 'POST Parameters Added',
    'POST-parameters-deleted': 'POST Parameters Deleted',
    'GET-summary-to': 'GET Summary To',  # added for demo
    'GET-summary-from': 'GET Summary From',  # added for demo
}

使用str.extract提取DICT的密钥,然后使用map替换为值:

pattern = fr"({'|'.join(re.escape(k) for k in keyword_label_mappings)})"

difference['labels'] = (
    difference['surface_wordings'].explode().str.extractall(pattern)[0]
                                  .map(keyword_label_mappings).droplevel('match')
                                  .groupby(level=0).agg(list)
)

输出:

>>> difference
                                                                      surface_wordings                              labels
63657  [paths-modified-/pets-operations-modified-GET-summary-from, paths-modified-/...  [GET Summary From, GET Summary To]
63658  [info-version-from, info-version-to, paths-modified-/pets-operations-modifie...  [GET Summary From, GET Summary To]
63659  [paths-modified-/pets-operations-modified-GET-summary-from, paths-modified-/...  [GET Summary From, GET Summary To]
63661  [info-title-from, info-title-to, info-license-deleted, info-version-from, in...  [GET Summary From, GET Summary To]
63662  [openAPI-from, openAPI-to, paths-added, paths-deleted, endpoints-added, endp...                                 NaN

Python相关问答推荐

Gekko解算器错误results.json未找到,无法找出原因

如何从同一类的多个元素中抓取数据?

FastAPI:使用APIRouter路由子模块功能

在Python中,如何才能/应该使用decorator 来实现函数多态性?

如何使用bs 4从元素中提取文本

Pandas 在时间序列中设定频率

如何销毁框架并使其在tkinter中看起来像以前的样子?

列表上值总和最多为K(以O(log n))的最大元素数

在函数内部使用eval(),将函数的输入作为字符串的一部分

Pandas 在最近的日期合并,考虑到破产

try 与gemini-pro进行多轮聊天时出错

如何比较numPy数组中的两个图像以获取它们不同的像素

试图找到Python方法来部分填充numpy数组

从numpy数组和参数创建收件箱

' osmnx.shortest_track '返回有效源 node 和目标 node 的'无'

如何在Django基于类的视图中有效地使用UTE和RST HTIP方法?

try 将一行连接到Tensorflow中的矩阵

如何获得每个组的时间戳差异?

我想一列Panadas的Rashrame,这是一个URL,我保存为CSV,可以直接点击

Pandas DataFrame中行之间的差异