如果特定日期的特定数量的数据出现在日期时间索引

2024-04-26 09:21:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者。我正在处理一个包含了几年数据的日期集。这是数据集的示例。你知道吗

enter image description here

这里,小时(LT)表示时间,DN(LT)表示一年中的天数。你知道吗

我已经尝试在python3.0中使用pandasanaconda来处理这个数据集。我的最终目标是找出每日,每周,每月和每年的平均数,所以我宁愿把它转换成日期时间索引(我想通过重新采样是很容易的!)你知道吗

我提供了迄今为止我所写的代码。你知道吗

import pandas as pd
import numpy as np

df = pd.read_csv('test_file.txt', sep=' ', delimiter=' ')

#convert the year, month, day int columns into datetime format 
year_month = pd.to_datetime(10000 * df.Year +100 * df.Month +df.Day, format='%Y%m%d')

#convert Year, Month, Day, Hour(LT) into DayTimeHour format
year_hour_convert = pd.DataFrame({
                                        'Day': np.array(year_month, dtype=np.datetime64), 
                                        'Hour': np.array(df['Hour(LT)'], dtype=np.int64)
                                })

#merge into "year-month-day-hour" format
year_hour = pd.to_datetime(year_hour_convert.Day) + pd.to_timedelta(year_hour_convert.Hour, unit='h')

#Define a new column for Time Series
df['DateTime'] = year_hour

#Drop unnecessary columns
df = df.drop(['Year', 'Month', 'Day', 'Hour(LT)', 'DN(LT)'], axis=1)

#Set YYYYMMDD HHMMSS as index
df = df.set_index('DateTime')

#Choose the data for 9 a.m. to 3 p.m.
df = df.between_time('09:00:00', '15:00:00')

我已经将数据集转换成这种格式。我最终放弃了'Year', 'Month', 'Day', 'Hour(LT)', 'DN(LT)'列。我提供了这种格式的图片。enter image description here

现在,如果某一天有一定数量的数据可用,我想过滤数据。例如,如果2016年1月4日以及2016-01-04的数据数大于4,我将取当天的数据。否则,我将删除当天的数据。你知道吗

我怎样才能在熊猫身上做到呢?你知道吗


Tags: to数据ltformatconvertdfnpyear