Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
 Pandas  Index.intersection()函數形成兩個Index對象的交集。這將返回一個新Index,其中包含該索引和其他元素的公用元素,從而保留調用索引的順序。
用法: Index.intersection(other)
參數:
other:索引或array-like
返回:交集:索引
範例1:采用Index.intersection()函數查找兩個索引的集合相交。
# importing pandas as pd 
import pandas as pd 
  
# Creating the first Index 
idx1 = pd.Index(['Labrador', 'Beagle', 'Mastiff',  
                     'Lhasa', 'Husky', 'Beagle']) 
  
# Creating the second Index 
idx2 = pd.Index(['Labrador', 'Great_Dane', 'Pug', 
           'German_sepherd', 'Husky', 'Pitbull']) 
  
# Print the first and second Index 
print(idx1, '\n', idx2)輸出:


現在我們找到兩個索引的集合相交。
# Find the elements common to both the Indexes 
idx2.intersection(idx1)輸出:

正如我們在輸出中看到的,Index.intersection()函數已返回兩個索引的交集。標簽的順序已根據調用索引進行了維護。
範例2:采用Index.intersection()函數查找兩個索引的集合相交。索引包含NaN值。
# importing pandas as pd 
import pandas as pd 
  
# Creating the first Index 
idx1 = pd.Index(['2015-10-31', '2015-12-02', None, '2016-01-03',  
                      '2016-02-08', '2017-05-05', '2014-02-11']) 
  
# Creating the second Index 
idx2 = pd.Index(['2015-10-31', '2015-10-02', '2018-01-03', 
           '2016-02-08', '2017-06-05', '2014-07-11', None]) 
  
# Print the first and second Index 
print(idx1, '\n', idx2)輸出:


現在我們找到idx1和idx2的交集。
# find intersection and maintain  
# ordering of labels based on idx1 
idx1.intersection(idx2)輸出:

注意:兩個索引中的缺失值被認為是彼此共有的。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Index.intersection()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
