I have a problem similar to this question but an opposite challenge. Instead of having a removal list, I have a keep list - a list of strings I'd like to keep. My question is how to use a keep list to filter out the unwanted strings and retain the wanted ones in the column.
import pandas as pd
df = pd.DataFrame(
{
"ID": [1, 2, 3, 4, 5],
"name": [
"Mitty, Kitty",
"Kandy, Puppy",
"Judy, Micky, Loudy",
"Cindy, Judy",
"Kitty, Wicky",
],
}
)
ID name
0 1 Mitty, Kitty
1 2 Kandy, Puppy
2 3 Judy, Micky, Loudy
3 4 Cindy, Judy
4 5 Kitty, Wicky
To_keep_lst = ["Kitty", "Kandy", "Micky", "Loudy", "Wicky"]