Возникли проблемы с повторением цикла - PullRequest
1 голос
/ 09 июля 2019

Я пытаюсь найти процентное совпадение между ключевыми словами, используя фильтры, и у меня возникли проблемы с получением правильного процентного результата при использовании цикла.

Вот что я пробовал до сих пор:

import pandas as pd

def percentmatch(component=[], manufacture=[]):
   dummy = 0
   for i in component:
       if i in manufacture:
           dummy += 1
   requirements = len(component)
   return (dummy/requirements)*100

def isDesired(innovator = [], manufacture = []):
   for i in innovator:
       if i in manufacture:
           return True
       return False

part = pd.read_csv("fakedata.csv")
#Change the Value for test case
part['Size'].iloc[5] = 'Startup'
manufacture = pd.read_csv("book1.csv")

#First filter if the manufacture wants to work with certain customer
criteria = []
for i, r in manufacture.iterrows():
    criteria.append((isDesired([part['Size'].iloc[0]], r['Desired Customer**'].split(", "))))
manufacture['criteria'] = criteria
firstfilter = manufacture[criteria]

Теперь второй фильтр.

#Second filter if the manufacture can do certain phase. Ex: prototype, pre-release
criteria2 = []
for i, r in firstfilter.iterrows():
    criteria2.append(isDesired([part['Phase'].iloc[0]], r['Preferred'].split(", ")))
firstfilter['criteria2'] = criteria2
secondfilter = firstfilter[criteria2]

#Third Filter to find the percent match in Methods
percentmatch1 = []
for i, r in secondfilter.iterrows():
    print(r['Method'].split(", "))
    print(part['Method'].iloc[0].split(", "))
   # Indentation below is there, but refuses to show in S.O. for some reason 
  percentmatch1.append(percentmatch([part['Method'].iloc[0].split(", ")], r['Method'].split(",")))
# End of for loop is above, next line is on same level of indentation as for loop instantiation
secondfilter['Method match'] = percentmatch1

В приведенном выше блоке кода мой вывод

['CNC Machining', '3D printing', 'Injection Molding']

['CNC Machining', '3D printing']

Выполнение быстрого поиска secondfilter.head () дает мне следующее:

secondfilter.head () вывод здесь

Совпадение метода должно составлять 100%, а не 0%. Как мне исправить это?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...