Reputation: 1106
I'm trying to add a column to the DF, depending on whether other column's value contains any of the strings in a list.
The list is:
services = [
"TELECOM",
"AYSA",
"PERSONAL"
]
And so far I've tried:
payments["category"] = "services" if payments["concept"].contains(service for service in services) else ""
And this:
payments["category"] = payments["concept"].apply(lambda x: "services" if x.contains(service) for service in services) else ""
Among some other variations... I've seen other questions but they're mostly related to the opposite problem (checking whether a column's value is contained by a string in a list)
I could use your help! Thanks!!
Upvotes: 1
Views: 2070
Reputation: 4592
i think you can use isin
payments['category'] = np.where(
payments['concept'].isin(services),
'services', '')
import pandas
import numpy
dic = {"concept": ["TELECOM", "NULL"]}
payments = pandas.DataFrame.from_dict(dic)
payments["category"] = numpy.where(payments["concept"].isin(["TELECOM", "AYSA", "PERSONAL"]), "services", "")
print(payments)
Upvotes: 1
Reputation: 150785
You can use np.where
and str.contains
:
payments['category'] = np.where(payments['concept'].str.contains('|'.join(services)),
'services', '')
Output:
concept category
0 TELECOM services
1 AYSA services
2 PERSONAL services
3 other things
Upvotes: 2