LoginSignup
0
0

More than 1 year has passed since last update.

pandasで同じコラムで結合(左右どっちかを優先)

Last updated at Posted at 2022-09-18

Memo

こちらの記事はメモとして、記載しました。

内容はとても簡単であり、pandasのドキュメントに書いてあることを書いただけです。
[pandasのドキュメント]
https://note.nkmk.me/python-pandas-merge-join/

code


import pandas as pd

df_left = pd.DataFrame({'a': ['a_1', 'a_2', 'a_3'], 'b': ['b_1', 'b_2', 'b_3']})
df_right = pd.DataFrame({'a': ['a_1', 'a_2', 'a_4'], 'c': ['c_1', 'c_2', 'c_4']})

print(df_left)
#      a    b
# 0  a_1  b_1
# 1  a_2  b_2
# 2  a_3  b_3

print(df_right)
#      a    c
# 0  a_1  c_1
# 1  a_2  c_2
# 2  a_4  c_4

"""
Key:1
howをleft又はrightとして因数に指定する場合
"""
pd.merge(df_left, df_rifht, on='a', how='left')

print(pd.merge(df_left, df_right, on='a', how='left'))
#      a    b    c
# 0  a_1  b_1  c_1
# 1  a_2  b_2  c_2
# 2  a_3  b_3  NaN

"""
Key:2
howをleft又はrightとして因数に指定しない場合
デフォルトは、howはinnerに指定されている。
"""
pd.merge(df_left, df_right, on='a')

print(pd.merge(df_left, df_right, on='a'))
#      a    b    c
# 0  a_1  b_1  c_1
# 1  a_2  b_2  c_2

解説

コード内にあるKey1では、how="left"に指定しています。

その結果、mergeで左側に指定しているデータフレーム(df_left)のデータを全て結合します。
df_rightはdf_right[df_right["a"] = Noneであるため、結合したデータフレームの二行目はNaNとなっています。

Key2では、howはデフォルトのinnerに指定されています。

その結果、二つのデータフレーム(df_left, df_right)の共通コラム("a")に対して、同じ値をとる行のみを結合しています。

0
0
1

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
0
0