Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python pandas how to update a column with value 1 if another column contains a certain word

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’ columm brand_cooltouch (1711) needs to be set to value 1 (int).
When Column description and keybenefits (14) contains the value ‘easylogic’ columm brand_easylogic (1712) needs to be set to value 1 (int).

Output that I want:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

One can use pandas.Series.str.contains.

For the string cooltouch do the following

df['brand_cooltouch (1711)'] = df['description and keybenefits (14)'].str.contains('cooltouch', case=False).astype(int)

[Out]:

    description and keybenefits (14)  brand_cooltouch (1711)  brand_easylogic (1712)
0  Lorem Ipsum cooltouch Lorem Ipsum                       1                     None
1  Lorem Ipsum easylogic Lorem Ipsum                       0                     None
2            Lorem Ipsum Lorem Ipsum                       0                     None

For the string easylogic, do the following

df['brand_easylogic (1712)'] = df['description and keybenefits (14)'].str.contains('easylogic', case=False).astype(int)

[Out]:

    description and keybenefits (14)  brand_cooltouch (1711)  brand_easylogic (1712)
0  Lorem Ipsum cooltouch Lorem Ipsum                       1                     0
1  Lorem Ipsum easylogic Lorem Ipsum                       0                     1
2            Lorem Ipsum Lorem Ipsum                       0                     0

Notes:

  • case=False is to make it case insensitive.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading