Python酷库之旅-第三方库Pandas(196)

目录

一、用法精讲

911、pandas.Index.join方法

911-1、语法

911-2、参数

911-3、功能

911-4、返回值

911-5、说明

911-6、用法

911-6-1、数据准备

911-6-2、代码示例

911-6-3、结果输出

912、pandas.Index.intersection方法

912-1、语法

912-2、参数

912-3、功能

912-4、返回值

912-5、说明

912-6、用法

912-6-1、数据准备

912-6-2、代码示例

912-6-3、结果输出

913、pandas.Index.union方法

913-1、语法

913-2、参数

913-3、功能

913-4、返回值

913-5、说明

913-6、用法

913-6-1、数据准备

913-6-2、代码示例

913-6-3、结果输出

914、pandas.Index.difference方法

914-1、语法

914-2、参数

914-3、功能

914-4、返回值

914-5、说明

914-6、用法

914-6-1、数据准备

914-6-2、代码示例

914-6-3、结果输出

915、pandas.Index.symmetric_difference方法

915-1、语法

915-2、参数

915-3、功能

915-4、返回值

915-5、说明

915-6、用法

915-6-1、数据准备

915-6-2、代码示例

915-6-3、结果输出

二、推荐阅读

1、Python筑基之旅

2、Python函数之旅

3、Python算法之旅

4、Python魔法之旅

5、博客个人主页

一、用法精讲

911、pandas.Index.join方法
911-1、语法
# 911、pandas.Index.join方法
final pandas.Index.join(other, *, how='left', level=None, return_indexers=False, sort=False)
Compute join_index and indexers to conform data structures to the new index.

Parameters:
other
Index
how
{‘left’, ‘right’, ‘inner’, ‘outer’}
level
int or level name, default None
return_indexers
bool, default False
sort
bool, default False
Sort the join keys lexicographically in the result Index. If False, the order of the join keys depends on the join type (how keyword).

Returns:
join_index, (left_indexer, right_indexer)
911-2、参数

911-2-1、other(必需)索引、数组或集合,表示要与当前索引比较的另一个索引、数组或集合。

911-2-2、how(可选,默认值为'left')字符串,指定连接的方式,选项包括'left'、'right'、'outer'、'inner',这些选项定义了在连接后索引的构成:

  • 'left':以调用的索引为基础,只保留左边索引中的元素。
  • 'right':以传入的索引为基础,只保留右边索引中的元素。
  • 'outer':保留两个索引中的所有元素,缺失的部分用 NaN 填充。
  • 'inner':只保留两个索引中都存在的元素。

911-2-3、level(可选,默认值为None)整数或字符串,指定要在多级索引中连接的级别,如果索引是多级的,可以传入特定的级别进行连接。

911-2-4、return_indexers(可选,默认值为False)布尔值,如果为True,则会返回索引器,将连接结果中的数据对应的原始位置索引器返回。

911-2-5、sort(可选,默认值为False)布尔值,如果为True,则对结果进行排序。

911-3、功能

        用于将两个索引进行连接,以便实现数据合并的需求,它可以用于连接Series、DataFrame或其他Index对象,提供了灵活的数据合并能力。

911-4、返回值

        返回一个新的Index对象,表示连接后的结果,如果return_indexers=True,则返回一个元组,其中包含新的索引和一个索引器,用于指示如何从原来的索引中获取结果,如果只返回索引,返回的类型取决于连接的类型以及如何连接(如可能是MultiIndex、Index等)。

911-5、说明

        无

911-6、用法
911-6-1、数据准备
911-6-2、代码示例
# 911、pandas.Index.join方法
import pandas as pd
idx1 = pd.Index([1, 2, 3])
idx2 = pd.Index([2, 3, 4])
# 左连接
result_left = idx1.join(idx2, how='left')
print(result_left)
# 右连接
result_right = idx1.join(idx2, how='right')
print(result_right)
# 外连接
result_outer = idx1.join(idx2, how='outer')
print(result_outer)
# 内连接
result_inner = idx1.join(idx2, how='inner')
print(result_inner)
911-6-3、结果输出
# 911、pandas.Index.join方法
# Index([1, 2, 3], dtype='int64')
# Index([2, 3, 4], dtype='int64')
# Index([1, 2, 3, 4], dtype='int64')
# Index([2, 3], dtype='int64')
912、pandas.Index.intersection方法
912-1、语法
# 912、pandas.Index.intersection方法
final pandas.Index.intersection(other, sort=False)
Form the intersection of two Index objects.

This returns a new Index with elements common to the index and other.

Parameters:
other
Index or array-like
sort
True, False or None, default False
Whether to sort the resulting index.

None : sort the result, except when self and other are equal or when the values cannot be compared.

False : do not sort the result.

True : Sort the result (which may raise TypeError).

Returns:
Index
912-2、参数

912-2-1、other(必需)索引、数组或集合,表示要与当前索引比较的另一个索引、数组或集合。

912-2-2、sort(可选,默认值为False)布尔值,如果设置为True,则对结果进行排序。

912-3、功能

        计算当前索引与另一个索引的交集,意味着结果将包含在两个索引中都出现的元素。

912-4、返回值

        返回一个新的Index对象,该对象包含在当前索引和other索引中都存在的元素,如果sort参数设置为True,则返回的索引将是排序的。

912-5、说明

        无

912-6、用法
912-6-1、数据准备
912-6-2、代码示例
# 912、pandas.Index.intersection方法
import pandas as pd
# 创建两个索引
index1 = pd.Index([1, 2, 3, 4, 5])
index2 = pd.Index([4, 5, 6, 7, 8])
# 计算交集
intersection = index1.intersection(index2, sort=False)
print(intersection)
912-6-3、结果输出
# 912、pandas.Index.intersection方法
# Index([4, 5], dtype='int64')
913、pandas.Index.union方法
913-1、语法
# 913、pandas.Index.union方法
final pandas.Index.union(other, sort=None)
Form the union of two Index objects.

If the Index objects are incompatible, both Index objects will be cast to dtype(‘object’) first.

Parameters:
other
Index or array-like
sort
bool or None, default None
Whether to sort the resulting Index.

None : Sort the result, except when

self and other are equal.

self or other has length 0.

Some values in self or other cannot be compared. A RuntimeWarning is issued in this case.

False : do not sort the result.

True : Sort the result (which may raise TypeError).

Returns:
Index
913-2、参数

913-2-1、other(必需)索引、数组或集合,表示要与当前索引比较的另一个索引、数组或集合。

913-2-2、sort(可选,默认值为None)布尔值,如果设置为True,则在返回结果前对结果进行排序;如果设置为False,不进行排序;如果为None,则根据输入的索引类型决定是否排序。

913-3、功能

        用于返回两个索引的并集,该方法能够创建一个包含两个索引中所有唯一元素的新索引,如果元素在两个索引中都存在,将只保留一次。

913-4、返回值

        返回一个新的Index对象,包含在两个索引中所有的唯一元素,结果根据sort参数的设置进行处理。

913-5、说明

        无

913-6、用法
913-6-1、数据准备
913-6-2、代码示例
# 913、pandas.Index.union方法
import pandas as pd
# 创建两个索引
index1 = pd.Index([1, 2, 3, 4])
index2 = pd.Index([3, 4, 5, 6])
# 计算并集
union_result = index1.union(index2, sort=False)
print(union_result)
913-6-3、结果输出
# 913、pandas.Index.union方法
# Index([1, 2, 3, 4, 5, 6], dtype='int64')
914、pandas.Index.difference方法
914-1、语法
# 914、pandas.Index.difference方法
final pandas.Index.difference(other, sort=None)
Return a new Index with elements of index not in other.

This is the set difference of two Index objects.

Parameters:
other
Index or array-like
sort
bool or None, default None
Whether to sort the resulting index. By default, the values are attempted to be sorted, but any TypeError from incomparable elements is caught by pandas.

None : Attempt to sort the result, but catch any TypeErrors from comparing incomparable elements.

False : Do not sort the result.

True : Sort the result (which may raise TypeError).

Returns:
Index
914-2、参数

914-2-1、other(必需)索引、数组或集合,表示要与当前索引比较的另一个索引、数组或集合。

914-2-2、sort(可选,默认值为None)布尔值,如果设置为True,则在返回结果前对结果进行排序;如果设置为False,不进行排序;如果为None,则根据输入的索引类型决定是否排序。

914-3、功能

        返回一个新的索引对象,其中包含在当前索引中但不在other索引中的所有元素。

914-4、返回值

        返回的结果是一个新的Index对象,包含了当前索引中独有的元素,如果没有找到差异,返回的索引将是空的。

914-5、说明

        无

914-6、用法
914-6-1、数据准备
914-6-2、代码示例
# 914、pandas.Index.difference方法
import pandas as pd
# 创建两个索引
index1 = pd.Index([1, 2, 3, 4, 5])
index2 = pd.Index([4, 5, 6, 7])
# 计算index1与index2的差异
diff = index1.difference(index2)
print(diff)
914-6-3、结果输出
# 914、pandas.Index.difference方法
# Index([1, 2, 3], dtype='int64')
915、pandas.Index.symmetric_difference方法
915-1、语法
# 915、pandas.Index.symmetric_difference方法
pandas.Index.symmetric_difference(other, result_name=None, sort=None)
Compute the symmetric difference of two Index objects.

Parameters:
other
Index or array-like
result_name
str
sort
bool or None, default None
Whether to sort the resulting index. By default, the values are attempted to be sorted, but any TypeError from incomparable elements is caught by pandas.

None : Attempt to sort the result, but catch any TypeErrors from comparing incomparable elements.

False : Do not sort the result.

True : Sort the result (which may raise TypeError).

Returns:
Index
Notes

symmetric_difference contains elements that appear in either idx1 or idx2 but not both. Equivalent to the Index created by idx1.difference(idx2) | idx2.difference(idx1) with duplicates dropped.
915-2、参数

915-2-1、other(必需)另一个索引(Index)对象,与当前索引进行对称差集计算。

915-2-2、result_name(可选,默认值为None)指定结果索引的名称。

915-2-3、sort(可选,默认值为None)指定结果索引的排序方式,如果不指定,默认为None,表示不排序。

915-3、功能

        计算两个索引之间的对称差集,即两个索引中不相交的元素的集合。换句话说,它返回一个新索引,包含两个索引中不相同的元素。

915-4、返回值

        返回一个新的索引(Index)对象,包含两个索引之间的对称差集。

915-5、说明

        无

915-6、用法
915-6-1、数据准备
915-6-2、代码示例
# 915、pandas.Index.symmetric_difference方法
import pandas as pd
# 创建两个索引
index1 = pd.Index(['A', 'B', 'C', 'D'])
index2 = pd.Index(['C', 'D', 'E', 'F'])
# 计算对称差集
result = index1.symmetric_difference(index2)
print(result)  
915-6-3、结果输出
# 915、pandas.Index.symmetric_difference方法
# Index(['A', 'B', 'E', 'F'], dtype='object')

二、推荐阅读

1、Python筑基之旅
2、Python函数之旅
3、Python算法之旅
4、Python魔法之旅
5、博客个人主页

猜你喜欢

转载自blog.csdn.net/ygb_1024/article/details/143496249