Python 3.x 如果另一列的对应行包含某个子字符串,则在列中指定一个字符串,否则指定另一个字符串
我有一个像这样的数据框Python 3.x 如果另一列的对应行包含某个子字符串,则在列中指定一个字符串,否则指定另一个字符串,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我有一个像这样的数据框 index | Creative Size | Business Model 1 | Something trueview | 2 | truviewhello | 3 | dunno | 4 | str | 5 | str | 我想写一个代码,如果在一列中有“trueview”将标签“CPV”分配给业务模型中的
index | Creative Size | Business Model
1 | Something trueview |
2 | truviewhello |
3 | dunno |
4 | str |
5 | str |
我想写一个代码,如果在一列中有“trueview”将标签“CPV”分配给业务模型中的相应行,否则它将分配“CPM”。
预期产出为:
index | Creative Size | Business Model
1 | Something trueview | CPV
2 | truviewhello | CPV
3 | dunno | CPM
4 | str | CPM
5 | str | CPM
我想到了这个:
count=0
for i in db_all['Creative Size']:
if 'trueview' in i:
db_all.loc[count, 'Business Model']='CPV'
else:
db_all.loc[count, 'Business Model']='CPM'
count = count+1
它可以工作,但速度很慢,有更好的主意吗?与以下设备一起使用:
db_all['Business Model'] = np.where(db_all['Creative Size'].str.contains('trueview'),
'CPV',
'CPM')