Skip to content
Advertisement

Label dataframe column with a list

I have a dataframe column text

JavaScript

I would like to create another column term by matching it with a list ['apple', 'banana, melon']

JavaScript

The result I get

JavaScript

However, I expected it to be

JavaScript

I sense that it might be because I use a list but I don’t know how to make a loop in lambda itself

Advertisement

Answer

Using the split method will only work if the strings are the same all the time. you have to switch around the loop and lambda expression like so

JavaScript

will return the matching value from the list

and will result in a output of

JavaScript

Edit: The reason you initial program doesn’t work is that your loop and lambda are mixed up. You are looping through the terms and applying only that term to the dataframe (ie your last execution of the loop is only checking for the term melon so banana and apple come up as none)

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