Copying (efficiently) dataframe content inside of another dataframe using Python
<p>I have one dataframe (<code>df1</code>):</p>

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

<p>I then create a new dataframe (<code>df2</code>) which has twice as many rows as <code>fd1</code>. My goal is to copy some elements of the first dataframe inside the second one in a smart way so that the result looks like this:</p>

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

<p>So far I was able to reach this goal by using the following commands:</p>

<pre><code>raw_data = {'A': ['pinco', 'pallo', 'pollo'], 
            'B': ['lollo', 'fallo', 'gollo'], 
            'C': ['pizzo', 'pazzo', 'razzo']}
df1 = pd.DataFrame(raw_data, columns = ['A', 'B', 'C'])
columns = ['XXX','YYY', 'ZZZ']
N = 3
df2 = pd.DataFrame(columns=columns,index=range(N*2))

idx = 0
for i in range(N):
    df2['XXX'].loc[idx] = df1['A'].loc[i]
    df2['XXX'].loc[idx+1] = df1['A'].loc[i]
    df2['YYY'].loc[idx] = df1['B'].loc[i]
    df2['YYY'].loc[idx+1] = df1['C'].loc[i]
    idx += 2
</code></pre>

<p>However I am looking for a more efficient (more compact and elegant) way to obtain this result. I tried to use the following combination inside of the for loop without success:</p>

<pre><code>df2[['XXX','YYY']].loc[idx] = df1[['A', 'B']].loc[i]
df2[['XXX','YYY']].loc[idx+1] = df1[['A', 'C']].loc[i]
</code></pre>

<p>Working from Nickil Maveli's answer, there's a faster (if somewhat more arcane) solution if you interleave B and C into a single array first. <a href="http://stackoverflow.com/questions/5347065/interweaving-two-numpy-arrays">(c. f. this question).</a></p>

<pre><code># Repeat elements in A twice
df2['XXX'] = np.repeat(df1['A'].values, 2)
# make a single interleaved array from the values of B and C and copy to YYYY
df2['YYY'] = np.dstack((df1['B'].values, df1['C'].values)).ravel() 
</code></pre>

<p>On my machine there was about a 3x speedup</p>

<pre><code>In [110]: %timeit df2.loc[::2, 'YYY'] = df1['B'].values; df2.loc[::2, 'YYY'] = df1['C'].values
1000 loops, best of 3: 274 Âµs per loop

In [111]: %timeit df2['YYY'] = np.dstack((df1['B'].values, df1['C'].values)).ravel()
10000 loops, best of 3: 87.5 Âµs per loop
</code></pre>

python pandas dataframe copy