[英]Check if each row in a pandas series contains a string from a list using apply?
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.我正在尝试向 DF 添加一列,具体取决于其他列的值是否包含列表中的任何字符串。
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!!
谢谢!!
You can use np.where
and str.contains
:您可以使用
np.where
和str.contains
:
payments['category'] = np.where(payments['concept'].str.contains('|'.join(services)),
'services', '')
Output: Output:
concept category
0 TELECOM services
1 AYSA services
2 PERSONAL services
3 other things
i think you can use isin我认为你可以使用 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.