Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统。 Pandas是其中的一种,使导入和分析数据更加容易。
 Pandas  dataframe.between_time()用于选择一天中特定时间(例如9:00-9:30 AM)之间的值。不像dataframe.at_time()函数,此函数提取时间范围内的值。此函数仅用于时间序列数据。 DataFrame 的索引必须是DatetimeIndex才能使用此函数。
用法: DataFrame.between_time(start_time, end_time, include_start=True, include_end=True)
参数:
start_time:datetime.time或字符串
end_time:datetime.time或字符串
include_start:布尔值,默认为True
include_end:布尔值,默认为True
返回:values_between_time:调用者类型
注意: between_time()当 DataFrame 的索引不是DatetimeIndex时,函数引发异常
范例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()函数。
相关用法
- 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 dataframe.between_time()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
