python - 如何更新列中的值?
问题描述
如果数据框的至少一个列包含以下单词之一,我将需要更改标签:
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
”中的标签。
你能告诉我如何改变它吗?
解决方案
尝试这个:
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')
希望这可以帮助 !!!
推荐阅读
- javascript - window.opener 在 chrome 版本 >88 中返回 null
- excel - 将值复制并粘贴到一个单元格中
- gunicorn - 在气流中添加额外的“异步”有什么用?
- python-3.x - 为什么在对测试数据使用转换后,我会收到特征数量的值错误?
- javascript - 在模板文字中,如何根据条件隐藏元素?
- mongodb - Mongoose 动态地将对象从不同集合中的数组添加到模式
- node.js - 快速网关记录每个请求
- next.js - 将 Next.js v10.1.13 更新到 webpack5 时,收到警告 Can't resolve 'fsevents' in chokidar
- angular - 观察者订阅不会将数据加载到数据源,使用材料表 Angular
- bash - Unix命令在嵌套目录下的多个文件中查找和替换字符串