Created
May 28, 2016 22:01
-
-
Save cpard/3070cb5d16e9021b46bec18ee346bb4f to your computer and use it in GitHub Desktop.
The implementation of the findGender function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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