I have a dataframe with some template strings and corresponding string variables to substitute. For example, given:
template,variable
"{color} shirt in {size}", "blue,medium"
"{capacity} bottle in {color}", "24oz,teal"
"{megapixel}mp camera", "24.1"
I'd like to produce the following:
"blue shirt in medium"
"24oz bottle in teal"
"24.1mp camera"
It's guaranteed that the number of template substrings in the first column will equal the number of variables in the string in the second column. The format for the strings is consistent to the examples above.
My first thought was to create a multindex dataframe using extractall and then joining :
templates = df['template'].str.extractall('({\w+\})')
variables = df['variable'].str.extractall('(\w+)')
multi_df = templates.join(variables, how='inner')
But I couldn't quite figure out where to go from there. Or is there perhaps an easier way?