I am using difflib's get_closest_matches to return N=3 best matches for each value in my input vector. I want to store the output in a single column in the dataframe, such as:
input output
"xyz" "xyz"
"xyz" "xzy"
"xyz" "xxy"
"pqr" "pqr"
...
What should I return from a call to apply that will automatically expand/broadcast the input to N outputs? For example, this will return the output as a list:
data["output"] = data["input"].apply(lambda x: difflib.get_close_matches(x, possibilities))
In this form, it would require many iterative calls to concat to unpack the list in each row. There must be a more straight-forward method that I am missing.
There are similar questions, such as this one Returning multiple values from pandas apply on a DataFrame, however they all expand the output into separate columns, whereas I need it in a single column.
Edit: As IanS correctly points out, possiblities
in this case is
possibilities = ['xyz', 'xzy', 'xxy', 'pqr']