📜  Python|熊猫 dataframe.between_time()

📅  最后修改于: 2022-05-13 01:55:14.424000             🧑  作者: Mango

Python|熊猫 dataframe.between_time()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

Pandas dataframe.between_time()用于选择一天中特定时间之间的值(例如上午 9:00-9:30)。与dataframe.at_time()函数不同,此函数提取时间范围内的值。此函数仅用于时间序列数据。 Dataframe 的索引必须是 DatetimeIndex 才能使用此函数。

注意:当数据帧的索引不是 DatetimeIndex 时, between_time()函数会引发异常

示例 #1:使用between_time()函数查找给定时间间隔之间的值。

# importing pandas as pd
import pandas as pd
  
# Creating row index values for dataframe
# Taken time frequency to be of 30 minutes interval
# Generating eight index value using "period = 8" parameter
ind = pd.date_range('01/01/2000', periods = 8, freq ='30T')
  
# Creating a dataframe with 2 columns
# using "ind" as the index for dataframe
  
df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 6, 7, 8],
                   "B":[10, 20, 30, 40, 50, 60, 70, 80]},
                                             index = ind)
  
# Printing the dataframe
df

现在让我们查询“02:00”到“03:30”之间的时间

# Find the row values between time "02:00" to "03:30"
df.between_time('02:00', '03:30')

输出 :
示例 #2:使用between_time()函数查找给定时间间隔之间的值,同时不包括开始时间和结束时间。

# importing pandas as pd
import pandas as pd
  
# Creating row index values for our data frame
# Taken time frequency to be of 30 minutes interval
# Generating eight index value using "period = 8" parameter
ind = pd.date_range('01/01/2000', periods = 8, freq ='30T')
  
# Creating a dataframe with 2 columns
# using "ind" as the index for our dataframe
  
df = pd.DataFrame({"A":[1, 2, 3, 4, 5, 6, 7, 8],
                   "B":[10, 20, 30, 40, 50, 60, 70, 80]},
                                             index = ind)
  
# query for time between "02:00" to "03:30" with
# both the start and end time values being excluded
df.between_time('02:00', '03:30', include_start = False,
                                    include_end = False)

输出 :

请注意,与开始时间和结束时间对应的值不包含在between_time()函数返回的数据帧中。