パンダのconcat()の例

パンダのconcat()メソッドは、データフレームやシリーズなどのパンダオブジェクトを連結するために使用されます。連結操作の動作を変更するために、さまざまなパラメータを渡すことができます。

1. パンダのconcat()の構文

concat()メソッドの構文は次の通りです。

concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False,
           keys=None, levels=None, names=None, verify_integrity=False,
           sort=None, copy=True)
  • objs: a sequence of pandas objects to concatenate.
  • join: optional parameter to define how to handle the indexes on the other axis. The valid values are ‘inner’ and ‘outer’.
  • join_axes: deprecated in version 0.25.0.
  • ignore_index: if True, the indexes from the source objects will be ignored and a sequence of indexes from 0,1,2…n will be assigned to the result.
  • keys: a sequence to add an identifier to the result indexes. It’s helpful in marking the source objects in the output.
  • levels: a sequence to specify the unique levels to create multiindex.
  • names: names for the levels in the resulting hierarchical index.
  • verify_integrity: Check whether the new concatenated axis contains duplicates. It’s an expensive operation.
  • sort: Sort non-concatenation axis if it is not already aligned when join is ‘outer’. Added in version 0.23.0
  • copy: if False, don’t copy data unnecessarily.

おすすめの読み物:PythonのPandasチュートリアル

2. Pandasのconcat()の例

簡単な例を見て、2つのDataFrameオブジェクトを連結する方法を見てみましょう。

import pandas

d1 = {"Name": ["Pankaj", "Lisa"], "ID": [1, 2]}
d2 = {"Name": "David", "ID": 3}

df1 = pandas.DataFrame(d1, index={1, 2})
df2 = pandas.DataFrame(d2, index={3})

print('********\n', df1)
print('********\n', df2)

df3 = pandas.concat([df1, df2])

print('********\n', df3)

出力:

********
      Name  ID
1  Pankaj   1
2    Lisa   2
********
     Name  ID
3  David   3
********
      Name  ID
1  Pankaj   1
2    Lisa   2
3   David   3

連結は行方向、つまり0軸で行われることに注意してください。また、ソースのDataFrameオブジェクトのインデックスは、出力でも保持されます。

3. 列方向に連結する、すなわち1軸方向での連結

d1 = {"Name": ["Pankaj", "Lisa"], "ID": [1, 2]}
d2 = {"Role": ["Admin", "Editor"]}

df1 = pandas.DataFrame(d1, index={1, 2})
df2 = pandas.DataFrame(d2, index={1, 2})

df3 = pandas.concat([df1, df2], axis=1)
print('********\n', df3)

出力:

********
      Name  ID    Role
1  Pankaj   1   Admin
2    Lisa   2  Editor

ソースオブジェクトに異なる種類のデータが含まれている場合、列方向の連結は意味があります。

4. 結合されたデータフレームのインデックスにキーを割り当てる。

d1 = {"Name": ["Pankaj", "Lisa"], "ID": [1, 2]}
d2 = {"Name": "David", "ID": 3}

df1 = pandas.DataFrame(d1, index={1, 2})
df2 = pandas.DataFrame(d2, index={3})

df3 = pandas.concat([df1, df2], keys=["DF1", "DF2"])
print('********\n', df3)

出力:

********
          Name  ID
DF1 1  Pankaj   1
    2    Lisa   2
DF2 3   David   3

5. 結合時にソースのデータフレームオブジェクトを無視する。

d1 = {"Name": ["Pankaj", "Lisa"], "ID": [1, 2]}
d2 = {"Name": "David", "ID": 3}

df1 = pandas.DataFrame(d1, index={10, 20})
df2 = pandas.DataFrame(d2, index={30})

df3 = pandas.concat([df1, df2], ignore_index=True)
print('********\n', df3)

出力:

********
      Name  ID
0  Pankaj   1
1    Lisa   2
2   David   3

これは、ソースオブジェクトのインデックスがあまり意味をなさない場合に便利です。そのため、これらを無視して出力データフレームにデフォルトのインデックスを割り当てることができます。

6.参考文献

  • pandas.concat() API Doc
コメントを残す 0

Your email address will not be published. Required fields are marked *