2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[python] pandas データフレームの結合 -concat-

Posted at

はじめに

業務自動化を進めているが、なんとかエクセルファイルからデータのみを取り出したとき、
抽出したデータを一つにまとめる方法をまとまる。

環境

window 10 home

import platform
print('python version: ' , platform.python_version())
# python version:  3.7.8
import pandas as pd
print('pandans version: ',pd.__version__)
# pandans version:  1.3.5

Code

データフレームの準備

df_1 = pd.DataFrame({'A': ['A1', 'A2', 'A3'],
                    'B': ['B1', 'B2', 'B3'],
                    'C': ['C1', 'C2', 'C3']},
                   index=['ONE', 'TWO', 'THREE'])
print(df_1)
"""
        A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
"""

df_2 = pd.DataFrame({'C': ['C1', 'C2', 'C3'],
                    'D': ['D1', 'D2', 'D3'],
                    'E': ['E1', 'E2', 'E3'],},
                   index=['ONE', 'TWO', 'THREE'])
print(df_2)
"""
        C   D   E
ONE    C1  D1  E1
TWO    C2  D2  E2
THREE  C3  D3  E3
"""

df_3 = pd.DataFrame({'D': ['D1', 'D2', 'D3'],
                    'E': ['E1', 'E2', 'E3'],
                    'F': ['F1', 'F2', 'F3']},
                   index=['first', 'second', 'thrid'])
print(df_3)
"""
         D   E   F
first   D1  E1  F1
second  D2  E2  F2
thrid   D3  E3  F3
"""

データフレームの結合 -Concat-

データフレーム(もしくはシリーズ)同士を結合する

df_concat = pd.concat([df_1, df_2])
print(df_concat)
"""
         A    B   C    D    E
ONE     A1   B1  C1  NaN  NaN
TWO     A2   B2  C2  NaN  NaN
THREE   A3   B3  C3  NaN  NaN
ONE    NaN  NaN  C1   D1   E1
TWO    NaN  NaN  C2   D2   E2
THREE  NaN  NaN  C3   D3   E3
"""

連結方向(縦、横)の指定 -axis-

縦はaxis = 0
横はaixs = 1
で指定する
指定がない場合はaxis = 0 となる

df_concat = pd.concat([df_1, df_2], axis=0)
print(df_concat)
"""
         A    B   C    D    E
ONE     A1   B1  C1  NaN  NaN
TWO     A2   B2  C2  NaN  NaN
THREE   A3   B3  C3  NaN  NaN
ONE    NaN  NaN  C1   D1   E1
TWO    NaN  NaN  C2   D2   E2
THREE  NaN  NaN  C3   D3   E3
"""


df_concat = pd.concat([df_1, df_1], axis=0)
print(df_concat)
"""
        A   B   C
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
ONE    A1  B1  C1
TWO    A2  B2  C2
THREE  A3  B3  C3
"""


df_concat = pd.concat([df_1, df_2], axis=1)
print(df_concat)
"""
        A   B   C   C   D   E
ONE    A1  B1  C1  C1  D1  E1
TWO    A2  B2  C2  C2  D2  E2
THREE  A3  B3  C3  C3  D3  E3
"""


df_concat = pd.concat([df_1, df_3], axis=1)
print(df_concat)
"""
          A    B    C    D    E    F
ONE      A1   B1   C1  NaN  NaN  NaN
TWO      A2   B2   C2  NaN  NaN  NaN
THREE    A3   B3   C3  NaN  NaN  NaN
first   NaN  NaN  NaN   D1   E1   F1
second  NaN  NaN  NaN   D2   E2   F2
thrid   NaN  NaN  NaN   D3   E3   F3
"""

連結方法(外部結合・内部結合)の指定 -join-

join='outer'であればすべてを含む(デフォルト)
join='inner'はNaNを含まない列もしくは行のみを残す

df_concat = pd.concat([df_1, df_2], axis=0, join='outer')
print(df_concat)
"""
         A    B   C    D    E
ONE     A1   B1  C1  NaN  NaN
TWO     A2   B2  C2  NaN  NaN
THREE   A3   B3  C3  NaN  NaN
ONE    NaN  NaN  C1   D1   E1
TWO    NaN  NaN  C2   D2   E2
THREE  NaN  NaN  C3   D3   E3
"""

df_concat = pd.concat([df_1, df_2], axis=0, join='inner')
print(df_concat)
"""
        C
ONE    C1
TWO    C2
THREE  C3
ONE    C1
TWO    C2
THREE  C3
"""

連結時の並び変え -sort-

sort=Flaseがデフォルトとして設定されている
sort=Trueにすることによって並び変えをする
axis=0の縦の結合なら列のみが
axis=1の横の結合なら行のみが
並び変えられる

df_4 = pd.DataFrame({'C': ['C3', 'C2', 'C1'],
                    'D': ['D3', 'D2', 'D1'],
                    'A': ['A3', 'A2', 'A1']},
                   index=[3, 2, 1])
                   
df_concat = pd.concat([df_4, df_4], axis=0)
print(df_concat)
"""
    C   D   A
3  C3  D3  A3
2  C2  D2  A2
1  C1  D1  A1
3  C3  D3  A3
2  C2  D2  A2
1  C1  D1  A1
"""

df_concat = pd.concat([df_4, df_4], axis=0, sort=True)
print(df_concat)
"""
    A   C   D
3  A3  C3  D3
2  A2  C2  D2
1  A1  C1  D1
3  A3  C3  D3
2  A2  C2  D2
1  A1  C1  D1
"""
             
df_concat = pd.concat([df_4, df_4], axis=1, sort=True)
print(df_concat)
"""
    C   D   A   C   D   A
1  C1  D1  A1  C1  D1  A1
2  C2  D2  A2  C2  D2  A2
3  C3  D3  A3  C3  D3  A3
"""

まとめ

ほとんどの場合は縦、横の指定だけで目的のほとんどは達成されるのではないかと思う。

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?