pandas join DF - merge vs. join不同的语义

时间:2017-04-17 14:36:10

标签: python pandas join

我想在熊猫中加入2 DF。有些列是int或float,其他列是类别。 (不对A和B df中的类别强制执行相同的cat代码/索引) 它们的常用列是大小为8的float和category列的列表。

加入

df_a.merge(df_b, how='inner'), on=join_columns )

根本不会返回任何结果。并通过

加入
df_a.join(df_b, lsuffix='_l', rsuffix='_r')

似乎工作。

但我有点困惑,为什么一个失败,如果我不应该将所有列都强制转换为对象,以防止cat代码加入可能是错误的。

即。如果left被选为merge的加入方法,则加入的列只会包含NAN个值。不幸的是,我不确定如何建立一个有用的最小例子。

修改

这里是一个样本

import pandas as pd

raw_data = {
        'subject_id': ['1', '2', '3', '4', '5'],
        'name': ['A', 'B', 'C', 'D', 'E'],
        'nationality': ['DE', 'AUT', 'US', 'US', 'US'],
        'age_group' : [1, 2, 1, 3, 1]}
df_a = pd.DataFrame(raw_data, columns = ['subject_id', 'name', 'nationality', 'age_group'])
df_a.nationality = df_a.nationality.astype('category')
df_a


raw_data = {
        'subject_id': ['1', '2', '3' ],
        'name': ['Billy', 'Brian', 'Bran'],
        'nationality': ['DE', 'US', 'US'],
        'age_group' : [1, 1, 3],
        'average_return_per_group' : [1.5, 2.3, 1.4]}
df_b = pd.DataFrame(raw_data, columns = ['subject_id', 'name', 'nationality', 'age_group', 'average_return_per_group'])
df_b.nationality = df_b.nationality.astype('category')
df_b


# some result is joined
df_a.join(df_b, lsuffix='_l', rsuffix='_r') 

# this *fails* as only NULL values joined, or nor result for inner join
df_a.merge(df_b, how='left', on=['nationality', 'age_group'])

2 个答案:

答案 0 :(得分:2)

join沿着索引默认加入,并且merge沿着具有相同名称的列加入。

检查一下:

In [115]: df_a.join(df_b, lsuffix='_l', rsuffix='_r')
Out[115]:
  subject_id_l name_l nationality_l  age_group_l subject_id_r name_r nationality_r  age_group_r average_returns_per_group
0            1      A            DE            1            1  Billy            DE          1.0                       NaN
1            2      B           AUT            2            2  Brian            US          1.0                       NaN
2            3      C            US            1            3   Bran            US          3.0                       NaN
3            4      D            US            3          NaN    NaN           NaN          NaN                       NaN
4            5      E            US            1          NaN    NaN           NaN          NaN                       NaN

让我们将['a','b','c']设置为df_b中的索引并尝试再次加入 - 您只会在所有NaN列中看到*_r

In [116]: df_a.join(df_b.set_index(pd.Index(['a','b','c'])), lsuffix='_l', rsuffix='_r')
Out[116]:
  subject_id_l name_l nationality_l  age_group_l subject_id_r name_r nationality_r  age_group_r average_returns_per_group
0            1      A            DE            1          NaN    NaN           NaN          NaN                       NaN
1            2      B           AUT            2          NaN    NaN           NaN          NaN                       NaN
2            3      C            US            1          NaN    NaN           NaN          NaN                       NaN
3            4      D            US            3          NaN    NaN           NaN          NaN                       NaN
4            5      E            US            1          NaN    NaN           NaN          NaN                       NaN

In [117]: df_b.set_index(pd.Index(['a','b','c']))
Out[117]:
  subject_id   name nationality  age_group average_returns_per_group
a          1  Billy          DE          1                       NaN
b          2  Brian          US          1                       NaN
c          3   Bran          US          3                       NaN

更新:IMO merge按预期工作(在文档中描述)

In [151]: df_a.merge(df_b, on=['nationality', 'age_group'], how='left', suffixes=['_l','_r'])
Out[151]:
  subject_id_l name_l nationality  age_group subject_id_r name_r  average_return_per_group
0            1      A          DE          1            1  Billy                       1.5
1            2      B         AUT          2          NaN    NaN                       NaN
2            3      C          US          1            2  Brian                       2.3
3            4      D          US          3            3   Bran                       1.4
4            5      E          US          1            2  Brian                       2.3

答案 1 :(得分:1)

我认为主要区别是join有默认left joinmerge inner join.