[英]Check if a string in a Pandas DataFrame column is in a list of strings
[英]Check if String in List of Strings is in DataFrame Pandas
我有一個關於將列表中的字符串匹配到 df 中的列的問題。
我讀了這個問題Check if String in List of Strings is in Pandas DataFrame Column並理解,但我的需求略有不同。
代碼:
Cars = {'Brand': ['Honda Civic','Toyota Corolla','Ford Focus','Audi A4', np.nan],
'Price': [22000,25000,27000,35000, 29000],
'Liscence Plate': ['ABC 123', 'XYZ 789', 'CBA 321', 'ZYX 987', 'DEF 456']}
df = pd.DataFrame(Cars,columns= ['Brand', 'Price', 'Liscence Plate'])
search_for_these_values = ['Honda', 'Toy', 'Ford Focus', 'Audi A4 2019']
pattern = '|'.join(search_for_these_values)
df['Match'] = df["Brand"].str.contains(pattern, na=False)
print (df)
Output 我得到:
Brand Price Liscence Plate Match
0 Honda Civic 22000 ABC 123 True
1 Toyota Corolla 25000 XYZ 789 True
2 Ford Focus 27000 CBA 321 True
3 Audi A4 35000 ZYX 987 False
4 NaN 29000 DEF 456 False
Output 我想要:
Brand Price Liscence Plate Match
0 Honda Civic 22000 ABC 123 True
1 Toyota Corolla 25000 XYZ 789 False
2 Ford Focus 27000 CBA 321 True
3 Audi A4 35000 ZYX 987 True
4 NaN 29000 DEF 456 False
使用單詞匹配的一種方法:
pat = "|".join(search_for_these_values).replace(" ", "|")
match = df["Brand"].str.findall(r"\b(%s)\b" % pat)
Output:
0 [Honda]
1 []
2 [Ford, Focus]
3 [Audi, A4]
4 NaN
Name: Brand, dtype: object
然后您可以將其分配回去
df["match"] = match.str.len().ge(1)
最終 output:
Brand Price Liscence Plate match
0 Honda Civic 22000 ABC 123 True
1 Toyota Corolla 25000 XYZ 789 False
2 Ford Focus 27000 CBA 321 True
3 Audi A4 35000 ZYX 987 True
4 NaN 29000 DEF 456 False
如果我們使用您概述的規則“如果一個詞為真,則為真”,那么這意味着如果品牌列中的一行有“2019”,那么將返回True
,我相信我們不希望這樣。 所以
話雖如此,您可以使用list comprehension
創建一個新列表,這是您的search_for_these_values
的先前split()
版本(不包括年份),並將isin
與any
一起使用:
# list comprehension
import re
s = [word for cars in search_for_these_values for word in cars.split() if not re.search(r'\d{4}',word)]
# Assign True / False
df['Match'] = df['Brand'].str.split(expand = True).isin(s).any(1)
打印回來:
Brand Price Liscence Plate Match
0 Honda Civic 22000 ABC 123 True
1 Toyota Corolla 25000 XYZ 789 False
2 Ford Focus 27000 CBA 321 True
3 Audi A4 35000 ZYX 987 True
4 NaN 29000 DEF 456 False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.