📜  Python|熊猫 dataframe.at_time()

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

Python|熊猫 dataframe.at_time()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.at_time()函数用于选择对应于当天输入时间的一行中的所有值。如果数据帧中不存在输入时间,则返回空数据帧。

注意:当数据帧的索引不是 DatetimeIndex 时,at_time()函数会引发异常
示例 #1:创建一个日期时间索引数据框并在任何特定时间检索值

Python3
# importing pandas as pd
import pandas as pd
 
# Creating row index values for dataframe
# Taken time frequency to be of 12 hours interval
 
# Generating five index value using "period = 5" parameter
ind = pd.date_range('01/ 01/2000', periods = 5, freq ='12H')
 
# Creating a dataframe with 2 columns
# using "ind" as the index for our dataframe
 
df = pd.DataFrame({"A":[1, 2, 3, 4, 5],
                   "B":[10, 20, 30, 40, 50]},
                                 index = ind)
 
# Printing the dataframe
# for visualization
df


Python3
df.at_time('12:00')


Python3
# importing pandas as pd
import pandas as pd
 
# Creating row index values for our data frame
# We have taken time frequency to be of 30 minutes interval
# We are 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)
 
# Printing the dataframe
df


Python3
# Find the row values at time "02:00"
df.at_time('02:00')


现在找出时间“12:00”的值

Python3

df.at_time('12:00')

输出 :


示例 #2:将 date_time 索引的频率设置为 30 分钟持续时间并查询有效和无效时间(数据帧中不存在)。

Python3

# importing pandas as pd
import pandas as pd
 
# Creating row index values for our data frame
# We have taken time frequency to be of 30 minutes interval
# We are 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)
 
# Printing the dataframe
df

现在让我们查询时间“02:00”

Python3

# Find the row values at time "02:00"
df.at_time('02:00')

输出 :