如何从python3中的pandas数据框中选择特定时间范围的列? [英] How to select column for a specific time range from pandas dataframe in python3?

查看:1097
本文介绍了如何从python3中的pandas数据框中选择特定时间范围的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的熊猫数据框

                     time    energy
0     2018-01-01 00:15:00    0.0000
1     2018-01-01 00:30:00    0.0000
2     2018-01-01 00:45:00    0.0000
3     2018-01-01 01:00:00    0.0000
4     2018-01-01 01:15:00    0.0000
5     2018-01-01 01:30:00    0.0000
6     2018-01-01 01:45:00    0.0000
7     2018-01-01 02:00:00    0.0000
8     2018-01-01 02:15:00    0.0000
9     2018-01-01 02:30:00    0.0000
10    2018-01-01 02:45:00    0.0000
11    2018-01-01 03:00:00    0.0000
12    2018-01-01 03:15:00    0.0000
13    2018-01-01 03:30:00    0.0000
14    2018-01-01 03:45:00    0.0000
15    2018-01-01 04:00:00    0.0000
16    2018-01-01 04:15:00    0.0000
17    2018-01-01 04:30:00    0.0000
18    2018-01-01 04:45:00    0.0000
19    2018-01-01 05:00:00    0.0000
20    2018-01-01 05:15:00    0.0000
21    2018-01-01 05:30:00    0.9392
22    2018-01-01 05:45:00    2.8788
23    2018-01-01 06:00:00    5.5768
24    2018-01-01 06:15:00    8.6660
25    2018-01-01 06:30:00   15.8648
26    2018-01-01 06:45:00   24.1760
27    2018-01-01 07:00:00   38.5324
28    2018-01-01 07:15:00   49.9292
29    2018-01-01 07:30:00   64.3788

我想使用特定时间范围 01:15:00-05:30:00 能源列中选择值,并对这些值求和.要从列中选择数据,我需要小时和分钟值.我知道如何分别使用小时和分钟从列中选择数据.

I would like to select the values from energy column using a specific Time range 01:15:00 - 05:30:00 and sum those values. To select datas from column I need both hour and minute values. I know how to select data from column using hour and minute separately..

import panadas as pd
from datetime import datetime as dt
energy_data = pd.read_csv("/home/mayukh/Downloads/Northam_january2018/output1.csv", index_col=None)
#Using Hour 
sum = energy_data[((energy_data.time.dt.hour < 1) & (energy_data.time.dt.hour >= 5))]['energy'].sum()
#using Minute
sum = energy_data[((energy_data.time.dt.minute < 15) & (energy_data.time.dt.minute >= 30))]['energy'].sum()

但是我不知道如何同时使用小时和分钟来选择数据.请告诉我如何进行.

but I don't know how to use both hour and minute together to select data. Please tell me the way how can I will proceed.

推荐答案

使用Datetimeindex一起使用.html"rel =" nofollow noreferrer> set_index :

Use between_time working with Datetimeindex created by set_index:

#if necessary convert to datetime
df['time'] = pd.to_datetime(df['time'])
a = df.set_index('time').between_time('01:15:00','05:30:00')['energy'].sum()
print (a)
0.9392

详细信息:

print (df.set_index('time').between_time('01:15:00','05:30:00'))
                     energy
time                       
2018-01-01 01:15:00  0.0000
2018-01-01 01:30:00  0.0000
2018-01-01 01:45:00  0.0000
2018-01-01 02:00:00  0.0000
2018-01-01 02:15:00  0.0000
2018-01-01 02:30:00  0.0000
2018-01-01 02:45:00  0.0000
2018-01-01 03:00:00  0.0000
2018-01-01 03:15:00  0.0000
2018-01-01 03:30:00  0.0000
2018-01-01 03:45:00  0.0000
2018-01-01 04:00:00  0.0000
2018-01-01 04:15:00  0.0000
2018-01-01 04:30:00  0.0000
2018-01-01 04:45:00  0.0000
2018-01-01 05:00:00  0.0000
2018-01-01 05:15:00  0.0000
2018-01-01 05:30:00  0.9392

这篇关于如何从python3中的pandas数据框中选择特定时间范围的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆