當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyspark Index.symmetric_difference用法及代碼示例

本文簡要介紹 pyspark.pandas.Index.symmetric_difference 的用法。

用法:

Index.symmetric_difference(other: pyspark.pandas.indexes.base.Index, result_name: Union[Any, Tuple[Any, …], None] = None, sort: Optional[bool] = None) → pyspark.pandas.indexes.base.Index

計算兩個 Index 對象的對稱差。

參數

other索引或類似數組
result_namestr
sort真或無,默認無

是否對結果索引進行排序。 * True:嘗試對結果進行排序。 * 無:不對結果進行排序。

返回

symmetric_difference index

注意

symmetric_difference 包含出現在 idx1idx2 但不是同時出現的元素。相當於idx1.difference(idx2) | idx2.difference(idx1) 創建的索引,刪除了重複項。

例子

>>> s1 = ps.Series([1, 2, 3, 4], index=[1, 2, 3, 4])
>>> s2 = ps.Series([1, 2, 3, 4], index=[2, 3, 4, 5])
>>> s1.index.symmetric_difference(s2.index)  
Int64Index([5, 1], dtype='int64')

您可以設置結果索引的名稱。

>>> s1.index.symmetric_difference(s2.index, result_name='pandas-on-Spark')  
Int64Index([5, 1], dtype='int64', name='pandas-on-Spark')

如果要對結果索引進行排序,可以將 sort 設置為 True

>>> s1.index.symmetric_difference(s2.index, sort=True)
Int64Index([1, 5], dtype='int64')

您還可以使用 ^ 運算符:

>>> s1.index ^ s2.index  
Int64Index([5, 1], dtype='int64')

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 pyspark.pandas.Index.symmetric_difference。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。