Skip to content
Advertisement

Is there any possibility to create a new column based on the keywords list

Is there any possibility to create a new column based on the keywords list?

JavaScript

I have data like this:

JavaScript

I would like to create a new column if the keyword exists in the Type column and if 2 keywords exist then the value should both keyword with a comma. I am having a problem because I have to check also location first and then type…

JavaScript

Is there any way other than if-else?

Advertisement

Answer

Let’s use groupby and map:

JavaScript

Details:

Create boolean mask with .isin to test for the values in Type that exists in keywords list:

JavaScript

filter the rows using the above mask and groupby on Location then agg Type using join:

JavaScript

.map the values from the above aggregated frame to the original df based on Location

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement