pandas.Index.union#
- final Index.union(other, sort=None)[源代码]#
形成两个Index对象的并集。
如果Index对象不兼容,则两个Index对象将首先转换为dtype(‘Object’)。
在 0.25.0 版更改.
- 参数
- other索引或类似数组
- sortBool或None,默认为None
是否对结果索引进行排序。
无:对结果进行排序,但以下情况除外
self 和 other 是平等的。
self 或 other 长度为0。
中的一些值 self 或 other 无法与之相比。在这种情况下,发出运行警告。
False:不对结果进行排序。
- 退货
- union索引
示例
联合匹配数据类型
>>> idx1 = pd.Index([1, 2, 3, 4]) >>> idx2 = pd.Index([3, 4, 5, 6]) >>> idx1.union(idx2) Int64Index([1, 2, 3, 4, 5, 6], dtype='int64')
联合不匹配的数据类型
>>> idx1 = pd.Index(['a', 'b', 'c', 'd']) >>> idx2 = pd.Index([1, 2, 3, 4]) >>> idx1.union(idx2) Index(['a', 'b', 'c', 'd', 1, 2, 3, 4], dtype='object')
多索引案例
>>> idx1 = pd.MultiIndex.from_arrays( ... [[1, 1, 2, 2], ["Red", "Blue", "Red", "Blue"]] ... ) >>> idx1 MultiIndex([(1, 'Red'), (1, 'Blue'), (2, 'Red'), (2, 'Blue')], ) >>> idx2 = pd.MultiIndex.from_arrays( ... [[3, 3, 2, 2], ["Red", "Green", "Red", "Green"]] ... ) >>> idx2 MultiIndex([(3, 'Red'), (3, 'Green'), (2, 'Red'), (2, 'Green')], ) >>> idx1.union(idx2) MultiIndex([(1, 'Blue'), (1, 'Red'), (2, 'Blue'), (2, 'Green'), (2, 'Red'), (3, 'Green'), (3, 'Red')], ) >>> idx1.union(idx2, sort=False) MultiIndex([(1, 'Red'), (1, 'Blue'), (2, 'Red'), (2, 'Blue'), (3, 'Red'), (3, 'Green'), (2, 'Green')], )