我有20000种产品的列表及其说明 This shows the variety of the products
我希望能够编写一个代码来搜索特定单词,例如“ TAPA” 并给出所有TAPA的输出
我找到了这个Find a specific word from a list in python,但是它使用了startswith
,它仅找到第一个项目,例如:
new = [x for x in df1['A'] if x.startswith('00320')]
## output ['00320671-01 Guide rail 25N/1660', '00320165S02 - Miniature rolling table']
我如何找到第二个字母,第三个字母或其他任何项目
PS-列表由字符串,整数,浮点数组成
答案 0 :(得分:1)
您可以为此使用string.find(substring)
。因此,在您的情况下,这应该可行:
new = [x for x in df1['A'] if x.find('00320') != -1]
find()
方法返回找到的子字符串的最低索引,否则返回-1。
要了解有关find()
用法的更多信息,请参阅Geeksforgeeks.com - Python String | find()
修改1: 正如@Thierry在评论中所建议的那样,更干净的方法是:
new = [x for x in df1['A'] if '00320' in x]
答案 1 :(得分:0)
您可以将熊猫的内置功能用于find partial string matches和generate lists:
new = df1['A'][df1['A'].astype(str).str.contains('00320')]['A'].tolist()
pandas str.contains()的一个优点是可以使用正则表达式。