Skip to content Skip to sidebar Skip to footer

Pandas: Make Function Map Partial Dict Match

This function looks at strings in a pandas DataFrame. If the string contains a regular expression matching an entry in the dictionary, it passes on the captured string to other par

Solution 1:

I think this might be what you are looking for.

df = pd.DataFrame(["BULL GOOGLE X3 VON", "BEAR TWITTER 12X S"], columns ["Name"])

#Dict
google = {"GOOG":"Google"}
twitter = {"TWITT":"Twitter"}
dictionary = google.copy()
dictionary.update(twitter)

#Regex
regex = re.compile(r"\b((%s)\S*)\b" %"|".join(dictionary.keys()), re.I)

def dictionary_lookup(match):
    return dictionary[match.group(2)]

#Function
def f(value):
    match = dictionary[regex.search(value).group(2)]
    #Do stuff
    statement = regex.sub(dictionary_lookup, value)
    return statement

#Map Function
df["Statement"] = df["Name"].map(lambda x:f(x))

This will match any word that starts with one of the keys in the dictionary, assign the value of the match from the dictionary to the variable match and then return the original string with the matched word replaced.


Post a Comment for "Pandas: Make Function Map Partial Dict Match"