重命名类别

2024-10-01 19:14:47 发布

您现在位置:Python中文网/ 问答频道 /正文

    text       category
----------------------------------------------- 
    nike    shoes from nike brought by ankit
    flour   grocery
    rice    grocery
    adidas  shoes from adidas are cool

以上是我的数据集格式。在分类的时候,我到底该如何概括这个范畴呢。 示例我希望输出为:-

^{pr2}$

Tags: 数据textfrombyarecategoryricecool
1条回答
网友
1楼 · 发布于 2024-10-01 19:14:47

一种方法是使用带有pd.DataFrame.apply的自定义函数:

import pandas as pd

df = pd.DataFrame({'text': ['nike', 'flour', 'rice', 'adidas'],
                   'category': ['shoes from nike bought by ankit', 'grocery', 'grocery',
                                'shoes from adidas are cool']})

def converter(row):
    if row['text'] in row['category']:
        return row['category'].split(' from ')[0] + ' from brand'
    else:
        return row['category']

df['category'] = df.apply(converter, axis=1)

#            category    text
# 0  shoes from brand    nike
# 1           grocery   flour
# 2           grocery    rice
# 3  shoes from brand  adidas

相关问题 更多 >

    热门问题