擅长:python、mysql、java
<h2>使用<code>map</code></h2>
<p>您需要将<code>Variables</code>和<code>Key</code>设置为映射数据帧的索引,然后简单地对列使用<strong><a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html" rel="nofollow noreferrer">map</a></strong>。在</p>
<pre><code>mapping_df = mapping_df.set_index(['Variables', 'Key'])
df = df.apply(lambda x: x.map(mapping_df.loc[x.name]['Values']))
</code></pre>
<p>同:</p>
^{pr2}$
<p>输出:</p>
<pre><code> GRTYPE CHRTSTAT SECTION
0 Total exclusions 4-year schools Revised cohort Bachelors/ equiv .
1 4-year institutions, Adjusted cohort Exclusions Bachelors/ equiv .
2 4-year institutions, Adjusted cohort Adjusted cohort Bachelors or equiv 2009 .
3 4-year institutions, Completers Revised cohort Bachelors/ equiv .
4 4-year institutions, Completers Exclusions Bachelors or equiv 2009 .
</code></pre>