Skip to content

Instantly share code, notes, and snippets.

@cpard
Created May 28, 2016 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpard/3070cb5d16e9021b46bec18ee346bb4f to your computer and use it in GitHub Desktop.
Save cpard/3070cb5d16e9021b46bec18ee346bb4f to your computer and use it in GitHub Desktop.
The implementation of the findGender function
def findGender(str):
f = fNames['name'].apply(lambda x: (x, SequenceMatcher(None,str.lower(),x).ratio()))
m = mNames['name'].apply(lambda x: (x, SequenceMatcher(None,str.lower(),x).ratio()))
f = padas.DataFrame([i for i in f], columns=['name','match']).sort_values('match', ascending=False)
m = padas.DataFrame([i for i in m], columns=['name','match']).sort_values('match', ascending=False)
maxMScore = m.iloc[0]['match']
maleName = m.iloc[0]['name']
maxFScore = f.iloc[0]['match']
femaleName = f.iloc[0]['name']
gender = np.nan
if maxFScore > maxMScore:
gender = 'female'
elif maxFScore < maxMScore:
gender = 'male'
else:
femScores = fNames.loc[fNames['name'] == femaleName].iloc[0]['freq']
memScores = mNames.loc[mNames['name'] == maleName].iloc[0]['freq']
if femScores > memScores:
gender = 'female'
elif femScores < memScores:
gender = 'male'
return gender
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment