使用python从数据帧中查找相似的项

2024-04-25 08:14:20 发布

您现在位置:Python中文网/ 问答频道 /正文

一个Python初学者。你知道吗

我有一份20000种产品的清单及其描述。我的任务是找到所有包含类似“00320047S01”或“02201179S02”的项目的产品。你知道吗

The list of products with highlighted products numbers

我试过了

df1 = pd.read_excel(r'C:..\Desktop\Book1.xlsx') # Read the excel file

df1['A'] = df1['A'].astype(str).str.lower() # To lower case the data

new = df1['A'][df1['A'].str.contains(r'00[0-9]{6}S0[0-9]{2}',regex=True)].tolist() # trying out regex  

[] #output

我哪里出错了?你知道吗

注:所有商品都没有随附图片所示的产品编号。你知道吗


Tags: ofthe项目产品withexcellowerlist
1条回答
网友
1楼 · 发布于 2024-04-25 08:14:20

尝试模式r'00\d+S0\d+r'00\d{6}S\d{2}'

例如:

import pandas as pd

df = pd.DataFrame({"A": ['00320047S01', '00201179S02', "Hello World"]})
print( df[df['A'].str.contains(r'00\d+S0\d+',regex=True)] )

输出:

             A
0  00320047S01
1  00201179S02

相关问题 更多 >