Python: Inserting DataFrames into larger DataFrame at given index value
<p>I'm certain this is a trivial question but I cannot seem to find a solution. I have the following DataFrame: </p>

<pre><code>&gt;&gt;&gt; df = pd.DataFrame({'A': [1, 2, 3, 4, 5],
                    'B': [1, 2, 3, 4, 5],
                    'C': [1, 2, 3, 4, 5]})
&gt;&gt;&gt; df
   A  B  C
0  1  1  1
1  2  2  2
2  3  3  3
3  4  4  4
4  5  5  5
</code></pre>

<p>I also have some additional DataFrames (<code>df_a</code>, <code>df_b</code>) that I wish to insert into the the primary DataFrame (<code>df</code>). The additional DataFrames are saved to a dictionary with the key being the index of the row that I wish to insert the additional DataFrames after. </p>

<pre><code>df_a = pd.DataFrame({'A': ['X', 'Y'],
                    'B': ['X', 'Y'],
                    'C': ['X', 'Y']})

df_b = pd.DataFrame({'A': ['X', 'Y'],
                    'B': ['X', 'Y'],
                    'C': ['X', 'Y']})

idx_dict = {}
idx_dict[1] = df_a
idx_dict[3] = df_b 
</code></pre>

<p>The final result would resemble this:</p>

<pre><code>&gt;&gt;&gt; result
   A  B  C
0  1  1  1
1  2  2  2
0  X  X  X
1  Y  Y  Y
2  3  3  3
3  4  4  4
0  X  X  X
1  Y  Y  Y
4  5  5  5
</code></pre>

<p>I've read through the python <a href="http://pandas.pydata.org/pandas-docs/stable/merging.html" rel="nofollow">concatenate</a> docs but can't seem to find out how to specify the particular location I wish to insert the DataFrame. Found a similar question <a href="http://stackoverflow.com/questions/20424932/concatenating-dataframes-by-index-columns-elements">here</a> but as my DataFrame indexes don't match this solution does not work for me.  </p>

<p>At least what you want can be achieved by slicing and concatenating:</p>

<pre><code>from collections import OrderedDict
idx_dict = OrderedDict(sorted(idx_dict.items()))
dfs = []
start_idx = 0
for idx in idx_dict:
    dfs.append(df.iloc[start_idx:idx + 1])
    dfs.append(idx_dict[idx])
    start_idx = idx + 1
dfs.append(df.iloc[start_idx:])
result = pd.concat(dfs)
</code></pre>

<p>I think "inserting" <code>df_a</code> and <code>df_b</code> is difficult because the pandas data is actually a numpy array, which cannot be resized.</p>

python pandas dictionary dataframe insert