df looks like this:
| description and keybenefits (14) | brand_cooltouch (1711) | brand_easylogic (1712) |
| ------------ | ------------ | ------------ |
| Lorem Ipsum cooltouch Lorem Ipsum | | |
| Lorem Ipsum easylogic Lorem Ipsum | | |
| Lorem Ipsum Lorem Ipsum | | |
What I want:
- When column description and keybenefits (14) contains the value 'cooltouch' column brand_cooltouch (1711) needs to be set to value 1 (int).
- When column description and keybenefits (14) contains the value 'easylogic' column brand_easylogic (1712) needs to be set to value 1 (int).
Output that I want:
| description and keybenefits (14) | brand_cooltouch (1711) | brand_easylogic (1712) |
| ------------ | ------------ | ------------ |
| Lorem Ipsum cooltouch Lorem Ipsum | 1 | |
| Lorem Ipsum Lorem Ipsum easylogic | | 1 |
| Lorem Ipsum Lorem Ipsum | | |
Any help is very much appreciated.
3条答案
按热度按时间8zzbczxx1#
可以使用
pandas.Series.str.contains
。对于字符串
cooltouch
,请执行以下操作对于字符串
easylogic
,请执行以下操作case=False
是使它不区分大小写。a8jjtwal2#
可以使用np.where。我建议用NaN或0填充所有不满足条件的单元格。下面是使用
np.nan
的解决方案v7pvogib3#
使用
Series.str.contains
-如果您不希望结果列是1和0-您还可以执行以下操作-