擅长:python、mysql、java
<p>给定以下输入:</p>
<pre><code>df_data = pd.DataFrame([['GoogleMaps', 100, 1], ['Google', 200, 2], ['PlayStore', 300, 3]], columns=['ad_group', 'cost', 'id'])
df_new_index = pd.DataFrame([['GoogleMaps', 'GMaps'], ['Google', 'GG'], ['PlayStore', 'PS']], columns=['ad_group', 'new_ad_group'])
</code></pre>
<p>请尝试以下单行代码:</p>
<pre><code>df_data.ad_group = df_data.ad_group.map(df_new_index.set_index('ad_group')['new_ad_group'])
</code></pre>
<p>并给出:</p>
<pre><code> ad_group cost id
0 GMaps 100 1
1 GG 200 2
2 PS 300 3
</code></pre>
<p>如果将索引设置为另一个数据帧上的<code>'ad_group'</code>列,则可以使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer">pandas.Series.map</a>函数替换原始数据帧<code>'ad_group'</code>列。你知道吗</p>