I have a column in pandas dataframe that looks like this:
Code
----
ABC,DEF,XYZ
ABC,XYZ
...
...
CBA,FED,ABC
I'm trying to check if this series of comma separated string contains any string in my below list:
["UVW","XYZ"]
I know we can check single value like "XYZ" in df["Code"] but how can we do it for a list of values in Python or is there any special functions from pandas?
Use pd.Series.str.contains
with regex=True
:
Given Series
, s
and target list l
:
s
0 ABC,DEF,XYZ
1 ABC,XYZ
2 CBA,FED,ABC
l = ["UVW","XYZ"]
s.str.contains('|'.join(l))
Output:
0 True
1 True
2 False
dtype: bool
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.