DataFrame同士を一定の方向についてそのままつなげる操作を連結
pandas.concat("DataFrameのリスト", axis=0)
とすることでリストの先頭から順に縦方向に連結
axis=1
を指定することで横方向に連結。
縦方向に連結するときは同じカラムについて連結され、
横方向に連結するときは同じインデックスについて連結されます。
1 |
<span role="presentation"><span class="cm-keyword">mport</span> <span class="cm-variable">pandas</span> <span class="cm-keyword">as</span> <span class="cm-variable">pd</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># 指定のインデックスとカラムを持つDataFrameを乱数によって作成する関数</span></span> |
1 |
<span role="presentation"><span class="cm-keyword">def</span> <span class="cm-def">make_random_df</span>(<span class="cm-variable">index</span>, <span class="cm-variable">columns</span>, <span class="cm-variable">seed</span>):</span> |
1 |
<span role="presentation"> <span class="cm-variable">np</span>.<span class="cm-property">random</span>.<span class="cm-property">seed</span>(<span class="cm-variable">seed</span>)</span> |
1 |
<span role="presentation"> <span class="cm-variable">df</span> = <span class="cm-variable">pd</span>.<span class="cm-property">DataFrame</span>()</span> |
1 |
<span role="presentation"> <span class="cm-keyword">for</span> <span class="cm-variable">column</span> <span class="cm-keyword">in</span> <span class="cm-variable">columns</span>:</span> |
1 |
<span role="presentation"> <span class="cm-variable">df</span>[<span class="cm-variable">column</span>] = <span class="cm-variable">np</span>.<span class="cm-property">random</span>.<span class="cm-property">choice</span>(<span class="cm-builtin">range</span>(<span class="cm-number">1</span>, <span class="cm-number">101</span>), <span class="cm-builtin">len</span>(<span class="cm-variable">index</span>))</span> |
1 |
<span role="presentation"> <span class="cm-variable">df</span>.<span class="cm-property">index</span> = <span class="cm-variable">index</span></span> |
1 |
<span role="presentation"> <span class="cm-keyword">return</span> <span class="cm-variable">df</span></span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-variable">columns</span> = [<span class="cm-string">"apple"</span>, <span class="cm-string">"orange"</span>, <span class="cm-string">"banana"</span>]</span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># df_data1とdf_data2を縦方向に連結しdf1に代入</span></span> |
1 |
<span role="presentation"><span class="cm-variable">df1</span>= <span class="cm-variable">pd</span>.<span class="cm-property">concat</span>([<span class="cm-variable">df_data1</span>,<span class="cm-variable">df_data2</span>])</span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-comment"># df_data1とdf_data2を横方向に連結しdf2に代入</span></span> |
1 |
<span role="presentation"><span class="cm-variable">df2</span> =<span class="cm-variable">pd</span>.<span class="cm-property">concat</span>([<span class="cm-variable">df_data1</span>,<span class="cm-variable">df_data2</span>], <span class="cm-variable">axis</span>=<span class="cm-number">1</span>)</span> |
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-variable">df1</span>)</span> |
1 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-variable">df2</span>)</span> |