pandas之DataFrame的连接函数join()介绍

pandas.DataFrame.join

原文参考于https://www.jianshu.com/p/2358d4013067
通过索引或者指定的列连接两个DataFrame。
DataFrame.join(other, on=None, how=’left’, lsuffix=”, rsuffix=”, sort=False)

参数说明

  • other:【DataFrame,或者带有名字的Series,或者DataFrame的list】如果传递的是Series,那么其name属性应当是一个集合,并且该集合将会作为结果DataFrame的列名
  • on:【列名称,或者列名称的list/tuple,或者类似形状的数组】连接的列,默认使用索引连接
  • how:【{‘left’, ‘right’, ‘outer’, ‘inner’}, default:‘left’】连接的方式,默认为左连接
  • lsuffix:【string】左DataFrame中重复列的后缀
  • rsuffix:【string】右DataFrame中重复列的后缀
  • sort:【boolean, default
    False】按照字典顺序对结果在连接键上排序。如果为False,连接键的顺序取决于连接类型(关键字)。

实例

现有first与other两个DataFrame对象

import pandas as pd
first=pd.DataFrame({'item_id':['a','b','c','b','d'],'item_price':[1,2,3,2,4]})
other=pd.DataFrame({'item_id':['a','b','f'],'item_atr':['k1','k2','k3']})
print(first)
print(other)

结果如下

  item_id  item_price
0       a           1
1       b           2
2       c           3
3       b           2
4       d           4
  item_id item_atr
0       a       k1
1       b       k2
2       f       k3
  1. 通过索引连接DataFrame
print(first.join(other, lsuffix='_left', rsuffix='_right'))

结果如下

  item_id_left  item_price item_id_right item_atr
0            a           1             a       k1
1            b           2             b       k2
2            c           3             f       k3
3            b           2           NaN      NaN
4            d           4           NaN      NaN
  1. 通过指定的列连接DataFrame
print(first.set_index('item_id').join(other.set_index('item_id')))

结果如下:

         item_price item_atr
item_id                     
a                 1       k1
b                 2       k2
b                 2       k2
c                 3      NaN
d                 4      NaN
  1. 通过on参数指定连接的列,DataFrame.join总是使用other的索引去连接first,因此我们可以把指定的列设置为other的索引,然后用on去指定first的连接列,这样可以让连接结果的索引和first一致
print(first.join(other.set_index('item_id'),on='item_id'))

结果如下:

  item_id  item_price item_atr
0       a           1       k1
1       b           2       k2
2       c           3      NaN
3       b           2       k2
4       d           4      NaN
  1. 右连接
print(first.join(other,how='right',lsuffix='_left',rsuffix='_right'))

结果如下:

  item_id_left  item_price item_id_right item_atr
0            a           1             a       k1
1            b           2             b       k2
2            c           3             f       k3
  1. 左连接
    同索引连接效果相同
print(first.join(other,how='left',lsuffix='_left',rsuffix='_right'))

结果如下:

  item_id_left  item_price item_id_right item_atr
0            a           1             a       k1
1            b           2             b       k2
2            c           3             f       k3
3            b           2           NaN      NaN
4            d           4           NaN      NaN
  1. 外连接
print(first.join(other.set_index('item_id'),on='item_id',how='outer'))

结果如下:

  item_id  item_price item_atr
0       a         1.0       k1
1       b         2.0       k2
3       b         2.0       k2
2       c         3.0      NaN
4       d         4.0      NaN
4       f         NaN       k3
  1. 内连接
print(first.join(other.set_index('item_id'),on='item_id',how='inner'))

结果如下:

  item_id  item_price item_atr
0       a           1       k1
1       b           2       k2
3       b           2       k2
  • 15
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
`join`函数PandasDataFrame对象用于合并的函数,它可以将两个或多个DataFrame对象按照指定的键(key)进行合并。其用法如下: ```python df1.join(df2, how='inner', on=None, lsuffix='', rsuffix='', sort=False) ``` 其中参数的含义如下: - `df1`:要进行合并的第一个DataFrame对象 - `df2`:要进行合并的第二个DataFrame对象 - `how`:指定合并方式,可取值为`left`、`right`、`outer`和`inner`,默认为`inner`。 - `left`:以左边DataFrame对象为基础,将右边DataFrame对象中符合条件的行合并到左边DataFrame对象中。 - `right`:以右边DataFrame对象为基础,将左边DataFrame对象中符合条件的行合并到右边DataFrame对象中。 - `outer`:将左右两个DataFrame对象中所有符合条件的行合并到一个新的DataFrame对象中,没有对应的行用NaN填充。 - `inner`:将左右两个DataFrame对象中符合条件的行合并到一个新的DataFrame对象中。 - `on`:指定合并使用的列名,如果不指定,则会自动使用两个DataFrame对象中相同的列名进行合并。 - `lsuffix`:左边DataFrame对象中的列名后缀,在列名有重复时使用。 - `rsuffix`:右边DataFrame对象中的列名后缀,在列名有重复时使用。 - `sort`:是否按照键(key)进行排序,默认为`False`。 示例: ```python import pandas as pd # 创建两个DataFrame对象 df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value1': [1, 2, 3, 4]}) df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value2': [5, 6, 7, 8]}) # 使用join函数进行合并 result = df1.join(df2.set_index('key'), on='key', how='inner') print(result) ``` 输出结果如下: ``` key value1 value2 1 B 2 5 3 D 4 6 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值