pandas.DataFrame: mappings a column of a list of keys to a column of the list of values
<p>Here is the input:</p>

<pre><code>df = pd.DataFrame({'keys': [('K0', 'K1'), ('K1', 'K2')],
                     'A': ['A0', 'A1']})
lookup_df = pd.DataFrame({'val': ['V1', 'V2', 'V3']},
                          index = ['K0', 'K1', 'K2'])
</code></pre>

<p>After some "join" operation, I'd like a new column be added to <code>df</code>, which maps the key in <code>keys</code> in <code>df</code> to the <code>val</code> in <code>lookup_df</code>.</p>

<p>The output should be:</p>

<pre><code>pd.DataFrame({'keys': [('K0', 'K1'), ('K1', 'K2')],
              'val': [('V0', 'V1'), ('V1', 'V2')],  
              'A': ['A0', 'A1']})
</code></pre>

<p>One way I can think of is:</p>

<pre><code>df['val'] = df['keys'].apply(lambda ks: 
    list(map(lambda k: lookup_df.loc[k].val, ks)))
</code></pre>

<p>Is there other better ways to achieve this?</p>

<p>Shorter and avoid string manipulation:</p>

<pre><code>df['val'] = df['keys'].apply(pd.Series).replace(lookup_df.val).apply(tuple)
</code></pre>

python pandas dataframe