首页 > 解决方案 > 如何更新列中的值?

问题描述

如果数据框的至少一个列包含以下单词之一,我将需要更改标签:

check_words=['pit','stop','PIT','STOP','Pit','Stop']

我的数据框中的行示例是:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.array([['Ferrari was hit by a radio communication blackout' , 'Scuderia Ferrari trying a double pit stop', ' If Ferrari takes nothing else away from the 2019 season, it must learn from its mistakes across the season'], ['We may use the following original news sources for stories', 'Sebastian Vettel insisted he trusts in Ferrari', 'During the recent Grand Prix of Italy, the Scuderia Ferrari team managed to execute one of the fastest pit stops ever performed during a Formula 1 race']]),
                   columns=['Text1', 'Short','Data'])

我创建了一个列标签,如下所示:

df['Label']='No Pit'识别列是否包含上面列表中的单词。如果它在该列表中包含一个单词,那么我需要更改“ Pit”中的标签。

你能告诉我如何改变它吗?

标签: pythonpandas

解决方案


尝试这个:

l = pd.DataFrame(np.vectorize(lambda r: any(x in r for x in check_words))(df.iloc[:3].values))
df['Label'] = l.any(1).agg(lambda x: 'pit' if x else 'not pit')

希望这可以帮助 !!!


推荐阅读