1

I am trying to find percent match between keywords using filters, and have had some trouble getting the correct percent result when using a loop.

Here's what I've tried so far:

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]

Now the second filter.

#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

In the above code block, my output is

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

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

Doing a quick secondfilter.head() lookup gives me the following:

secondfilter.head() output here

The method match should be 100% not 0%. How do I correct this?

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
mike0494
  • 21
  • 3
  • Can you provide some sample input and preferred output? Please see [How to create good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – G. Anderson Jul 09 '19 at 19:30

0 Answers0