2024-10-01 19:14:47 发布
网友
text category ----------------------------------------------- nike shoes from nike brought by ankit flour grocery rice grocery adidas shoes from adidas are cool
以上是我的数据集格式。在分类的时候,我到底该如何概括这个范畴呢。 示例我希望输出为:-
一种方法是使用带有pd.DataFrame.apply的自定义函数:
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
一种方法是使用带有
pd.DataFrame.apply
的自定义函数:相关问题 更多 >
编程相关推荐