Python 如何在我搜索的关键字的数据框中显示/打印或排序元素?
我想跟踪体育器材库存,它由3个品牌、产品及其价格数组组成,我将其存储在一个数据框中,并使用FuzzyWuzzy搜索特定关键字(例如“Football”),在我的pandas数据框中,它应仅显示我的数据框中的这些元素 所以有一个结果数组,它由我搜索过的关键字的元组和关键字出现在列表中的概率组成,所以我只想在结果数据框中显示这些元素Python 如何在我搜索的关键字的数据框中显示/打印或排序元素?,python,pandas,dataframe,Python,Pandas,Dataframe,我想跟踪体育器材库存,它由3个品牌、产品及其价格数组组成,我将其存储在一个数据框中,并使用FuzzyWuzzy搜索特定关键字(例如“Football”),在我的pandas数据框中,它应仅显示我的数据框中的这些元素 所以有一个结果数组,它由我搜索过的关键字的元组和关键字出现在列表中的概率组成,所以我只想在结果数据框中显示这些元素 from fuzzywuzzy import process from pandas import DataFrame Brands=['Nike', 'Adidas
from fuzzywuzzy import process
from pandas import DataFrame
Brands=['Nike', 'Adidas', 'Nike', 'Puma', 'UnderArmour', 'Adidas', 'Puma', 'New Balance', 'Puma']
Product=['Phantom Football Boots',
'Football Shorts',
'Running Shoes',
'Evospeed Football shoes',
'Gym Gloves',
'Running Shoes',
'Cap',
'Furon Football Boots',
'Football Size 1']
Price_USD=[200, 45, 100, 90, 30, 120,12,35,20]
stocks_df={'Brands':Brands,
'Product':Product,
'Price':Price_USD
}
searchproduct=input("Enter Product Name: ")
def getSearchedProducts(searchproduct, choices, limit=5):
res=process.extract(searchproduct, choices, limit=limit)
return res
result=getSearchedProducts(searchproduct, Product)
df=DataFrame(stocks_df, columns=['Brands', 'Product', 'Price'])
因此,我的数据帧应该如下所示:
Brands Product Price
0 Nike Phantom Football Boots 200
1 Adidas Football Shorts 45
2 Nike Running Shoes 100
3 Puma Evospeed Football shoes 90
4 UnderArmour Gym Gloves 30
5 Adidas Running Shoes 120
6 Puma Cap 12
7 New Balance Furon Football Boots 35
8 Puma Football Size 1 20
您可以筛选结果以生成得分为X%或更高(取决于您希望的准确程度)的产品列表,然后仅从数据框中选择具有此产品的行
from fuzzywuzzy import process
from pandas import DataFrame
Brands=['Nike', 'Adidas', 'Nike', 'Puma', 'UnderArmour', 'Adidas', 'Puma', 'New Balance', 'Puma']
Product=['Phantom Football Boots',
'Football Shorts',
'Running Shoes',
'Evospeed Football shoes',
'Gym Gloves',
'Running Shoes',
'Cap',
'Furon Football Boots',
'Football Size 1']
Price_USD=[200, 45, 100, 90, 30, 120,12,35,20]
stocks_df={'Brands':Brands,
'Product':Product,
'Price':Price_USD
}
searchproduct=input("Enter Product Name: ")
def getSearchedProducts(searchproduct, choices, limit=5):
res=process.extract(searchproduct, choices, limit=limit)
return res
result=getSearchedProducts(searchproduct, Product)
df=DataFrame(stocks_df, columns=['Brands', 'Product', 'Price'])
#form a list of products which has a score of 80% or more
product_result_list = [res[0] for res in result if res[1]>=80]
#create a new df of only those products from our list.
product_result_df = df[df['Product'].isin(product_result_list)]
print(product_result_df)
输出:当输入以80分或以上的分数给出时
Enter Product Name: boots
Brands Product Price
0 Nike Phantom Football Boots 200
7 New Balance Furon Football Boots 35