How to copy one DataFrame column in to another Dataframe if their indexes values are the same
<p>After creating a DataFrame with some duplicated cell values in column with the name 'keys':</p>

<pre><code>import pandas as pd
df = pd.DataFrame({'keys': [1,2,2,3,3,3,3],'values':[1,2,3,4,5,6,7]})
</code></pre>

<p><a href="http://i.stack.imgur.com/M9c0P.png" rel="nofollow"><img src="http://i.stack.imgur.com/M9c0P.png" alt="enter image description here"></a></p>

<p>I go ahead and create two more DataFrames which are the consolidated versions of the original DataFrame <code>df</code>. Those newly created DataFrames will have no duplicated cell values under the 'keys' column:</p>

<pre><code>df_sum = df_a.groupby('keys', axis=0).sum().reset_index()
df_mean = df_b.groupby('keys', axis=0).mean().reset_index()
</code></pre>

<p>As you can see <code>df_sum['values']</code> cells values were all summed together.
While <code>df_mean['values']</code> cell values were averaged with <code>mean()</code> method.
Lastly I rename the 'values' column in both dataframes with:</p>

<pre><code>df_sum.columns = ['keys', 'sums']
df_mean.columns = ['keys', 'means']
</code></pre>

<p><a href="http://i.stack.imgur.com/oKmxV.png" rel="nofollow"><img src="http://i.stack.imgur.com/oKmxV.png" alt="enter image description here"></a></p>

<p>Now I would like to copy the <code>df_mean['means']</code> column into the dataframe <code>df_sum</code>.</p>

<p>How to achieve this?</p>

<p>The Photoshoped image below illustrates the dataframe I would like to create. Both 'sums' and 'means' columns are merged into a single DataFrame:</p>

<p><a href="http://i.stack.imgur.com/Ws6n9.png" rel="nofollow"><img src="http://i.stack.imgur.com/Ws6n9.png" alt="enter image description here"></a></p>

<p>There are several ways to do this.  Using the <code>merge</code> function off the dataframe is the most efficient.</p>

<pre><code>df_both = df_sum.merge(df_mean, how='left', on='keys')

df_both

Out[1]:
   keys  sums  means
0     1     1    1.0
1     2     5    2.5
2     3    22    5.5
</code></pre>

python pandas