I'm trying to do a bit of cleanse to some data sets, I can accomplish the task with some for loops but I wanted a more pythonic/pandorable way to do this.
This is the code I came up with, the data is not real..but it should work
import pandas as pd
# This is a dataframe containing the correct values
correct = pd.DataFrame([{"letters":"abc","data":1},{"letters":"ast","data":2},{"letters":"bkgf","data":3}])
# This is the dataframe containing source data
source = pd.DataFrame([{"c":"ab"},{"c":"kh"},{"c":"bkg"}])
for i,word in source["c"].iteritems():
for j,row in correct.iterrows():
if word in row["letters"]:
source.at[i,"c"] = row["data"]
break
This is my attempt to a pandorable way but it fails because of the list comprehension returning a generator:
source["c"] = source["c"].apply(
lambda x: row["data"] if x in row["letters"] else x for row in
correct.iterrows()
)