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


Python Pandas TimedeltaIndex.intersection用法及代碼示例

Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。

Pandas TimedeltaIndex.intersection()函數返回一個新的Index,其中包含來自兩個索引的元素。這是TimedeltaIndex對象的專用交集。它可能比Index.intersection快得多。

用法: TimedeltaIndex.intersection(other)

參數:
other:TimedeltaIndex或array-like

返回:索引或TimedeltaIndex

範例1:采用TimedeltaIndex.intersection()函數查找兩個TimedeltaIndex對象的交集。

# importing pandas as pd 
import pandas as pd 
  
# Create the first TimedeltaIndex object 
tidx1 = pd.TimedeltaIndex(start ='11 days 22:11:12.001124', periods = 5, 
                                          freq ='T', name ='New_object') 
  
# Create the seccond TimedeltaIndex object 
tidx2 = pd.TimedeltaIndex(start ='11 days 22:14:12.001124', periods = 5, 
                                          freq ='T', name ='New_object') 
  
# Print the first and second TimedeltaIndex object 
print(tidx1, '\n', tidx2)

輸出:

現在我們將使用TimedeltaIndex.intersection()查找兩個對象的交集的函數

# find the intersection 
tidx1.intersection(tidx2)

輸出:

正如我們在輸出中看到的,TimedeltaIndex.intersection()函數返回了一個僅包含tidx1和tidx2共同元素的對象。

範例2:采用TimedeltaIndex.intersection()函數查找兩個TimedeltaIndex對象的交集。

# importing pandas as pd 
import pandas as pd 
  
# Create the first TimedeltaIndex object 
tidx1 = pd.TimedeltaIndex(start ='1 days 02:00:12.001124', 
                    periods = 5, freq ='D', name ='Koala') 
  
# Create the second TimedeltaIndex object 
tidx2 = pd.TimedeltaIndex(start ='3 days 02:00:12.001124',  
                    periods = 5, freq ='D', name ='Koala') 
  
# Print the first and second TimedeltaIndex object 
print(tidx1, '\n', tidx2)

輸出:

現在我們將使用TimedeltaIndex.intersection()查找兩個對象的交集的函數

# find the intersection 
tidx1.intersection(tidx2)

輸出:

正如我們在輸出中看到的,TimedeltaIndex.intersection()函數返回了一個僅包含tidx1和tidx2共同元素的對象。



相關用法


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