内部結合とは共通するデータのみを結合し、
共通しないデータは破棄される
■データフレーム作成
1 |
<span role="presentation"></span> |
1 |
<span role="presentation"><span class="cm-variable">data1</span> = {<span class="cm-string">"fruits"</span>: [<span class="cm-string">"apple"</span>, <span class="cm-string">"orange"</span>, <span class="cm-string">"banana"</span>, <span class="cm-string">"strawberry"</span>, <span class="cm-string">"kiwifruit"</span>],</span> |
1 |
<span role="presentation"> <span class="cm-string">"year"</span>: [<span class="cm-number">2001</span>, <span class="cm-number">2002</span>, <span class="cm-number">2001</span>, <span class="cm-number">2008</span>, <span class="cm-number">2006</span>],</span> |
1 |
<span role="presentation"> <span class="cm-string">"amount"</span>: [<span class="cm-number">1</span>, <span class="cm-number">4</span>, <span class="cm-number">5</span>, <span class="cm-number">6</span>, <span class="cm-number">3</span>]}</span> |
1 |
<span role="presentation"><span class="cm-variable">df1</span> = <span class="cm-variable">pd</span>.<span class="cm-property">DataFrame</span>(<span class="cm-variable">data1</span>)</span> |
1 |
1 2 3 4 |
<span role="presentation"><span class="cm-builtin">print</span>(<span class="cm-variable">df1</span>) </span> |
1 2 3 4 5 6 |
amount fruits year 0 1 apple 2001 1 4 orange 2002 2 5 banana 2001 3 6 strawberry 2008 4 3 kiwifruit 2006 |
1 |
<span role="presentation"> </span> |
1 |
■データフレーム縦方向に直接結合
# df_data1とdf_data2を縦方向に連結しdf1に代入
df1 = pd.concat([df_data1, df_data2], axis=0)
apple orange banana
1 45 68 37
2 48 10 88
3 65 84 71
4 68 22 89
1 38 76 17
2 13 6 2
3 73 80 77
4 10 65 72
■データフレーム横方向に直接結合
# df_data1とdf_data2を横方向に連結しdf1に代入
df2 = pd.concat([df_data1, df_data2], axis=1)
apple orange banana apple orange banana
1 45 68 37 38 76 17
2 48 10 88 13 6 2
3 65 84 71 73 80 77
4 68 22 89 10 65 72
1 |
コメントを残す