获取每30分钟的行数值?

huangapple go评论103阅读模式
英文:

How to get the row values for every 30th minute?

问题

我有一个数据集,它是一个数据框:

	item	id	_value	agentstatus	type	agenttimestamp	Unit	Calc1	Calc2
0	FCX.FCN2.PM01001_01_SUM.SUM	1	3	Good	Double	2022-01-01 09:00:00+00:00	kWh	Diff	30min
1	FCX.FCN2.PM01002_01_SUM.SUM	2	3	Good	Double	2022-01-01 09:00:00+00:00	kWh	Diff	30min
25	FCX.FCN2.PM01001_01_SUM.SUM	26	3	Good	Double	2022-01-01 09:10:00+00:00	kWh	Diff	30min
26	FCX.FCN2.PM01002_01_SUM.SUM	27	3	Good	Double	2022-01-01 09:10:00+00:00	kWh	Diff	30min
50	FCX.FCN2.PM01001_01_SUM.SUM	51	3	Good	Double	2022-01-01 09:20:00+00:00	kWh	Diff	30min
51	FCX.FCN2.PM01002_01_SUM.SUM	52	3	Good	Double	2022-01-01 09:20:00+00:00	kWh	Diff	30min
75	FCX.FCN2.PM01001_01_SUM.SUM	76	3	Good	Double	2022-01-01 09:30:00+00:00	kWh	Diff	30min
76	FCX.FCN2.PM01002_01_SUM.SUM	77	3	Good	Double	2022-01-01 09:30:00+00:00	kWh	Diff	30min
100	FCX.FCN2.PM01001_01_SUM.SUM	101	3	Good	Double	2022-01-01 09:40:00+00:00	kWh	Diff	30min
101	FCX.FCN2.PM01002_01_SUM.SUM	102	3	Good	Double	2022-01-01 09:40:00+00:00	kWh	Diff	30min
125	FCX.FCN2.PM01001_01_SUM.SUM	126	3	Good	Double	2022-01-01 09:50:00+00:00	kWh	Diff	30min
126	FCX.FCN2.PM01002_01_SUM.SUM	127	3	Good	Double	2022-01-01 09:50:00+00:00	kWh	Diff	30min
150	FCX.FCN2.PM01001_01_SUM.SUM	151	3	Good	Double	2022-01-01 10:00:00+00:00	kWh	Diff	30min
151	FCX.FCN2.PM01002_01_SUM.SUM	152	3	Good	Double	2022-01-01 10:00:00+00:00	kWh	Diff	30min
175	FCX.FCN2.PM01001_01_SUM.SUM	176	3	Good	Double	2022-01-01 10:10:00+00:00	kWh	Diff	30min
176	FCX.FCN2.PM01002_01_SUM.SUM	177	3	Good	Double	2022-01-01 10:10:00+00:00	kWh	Diff	30min
200	FCX.FCN2.PM01001_01_SUM.SUM	201	3	Good	Double	2022-01-01 10:20:00+00:00	kWh	Diff	30min
201	FCX.FCN2.PM01002_01_SUM.SUM	202	3	Good	Double	2022-01-01 10:20:00+00:00	kWh	Diff	30min
225	FCX.FCN2.PM01001_01_SUM.SUM	226	3	Good	Double	2022-01-01 10:30:00+00:00	kWh	Diff	30min
226	FCX.FCN2.PM01002_01_SUM.SUM	227	3	Good	Double	2022-01-01 10:30:00+00:00	kWh	Diff	30min
250	FCX.FCN2.PM01001_01_SUM.SUM	251	3	Good	Double	2022-01-01 10:40:00+00:00	kWh	Diff	30min
251	FCX.FCN2.PM01002_01_SUM.SUM	252	3	Good	Double	2022-01-01 10:40:00+00:00	kWh	Diff	30min
275	FCX.FCN2.PM01001_01_SUM.SUM	276	3	Good	Double	2022-01-01 10:50:00+00:00	kWh	Diff	30min
276	FCX.FCN2.PM01002_01_SUM.SUM	277	3	Good	Double	2022-01-01 10:50:00+00:00	kWh	Diff	30min
300	FCX.FCN2.PM01001_01_SUM.SUM	301	3	Good	Double	2022-01-01 11:00:00+00:00	kWh	Diff	30min
301	FCX.FCN2.PM01002_01_SUM.SUM	302	3	Good	Double	2022-01-01 11:00:00+00:00	kWh	Diff	30min
325	FCX.FCN2.PM01001_01_SUM.SUM	326	3	Good	Double	2022-01-01 11:10:00+00:00	kWh	Diff	30min
326	FCX.FCN2.PM01002_01_SUM.SUM	327	3	Good	Double	2022-01-01 11:10:00+00:00	kWh	Diff	30min
350	FCX.FCN2.PM01001_01_SUM.SUM	351	3	Good	Double	2022-01-01 11:20:00+00:00	kWh	Diff	30min
351	FCX.FCN2.PM01002_01_SUM.SUM	352	3	Good	Double	2022-01-01 11:20:00+00:00	kWh	Diff	30min
英文:

I have a dataset, it's a dataframe:

	item	id	_value	agentstatus	type	agenttimestamp	Unit	Calc1	Calc2
0	FCX.FCN2.PM01001_01_SUM.SUM	1	3	Good	Double	2022-01-01 09:00:00+00:00	kWh	Diff	30min
1	FCX.FCN2.PM01002_01_SUM.SUM	2	3	Good	Double	2022-01-01 09:00:00+00:00	kWh	Diff	30min
25	FCX.FCN2.PM01001_01_SUM.SUM	26	3	Good	Double	2022-01-01 09:10:00+00:00	kWh	Diff	30min
26	FCX.FCN2.PM01002_01_SUM.SUM	27	3	Good	Double	2022-01-01 09:10:00+00:00	kWh	Diff	30min
50	FCX.FCN2.PM01001_01_SUM.SUM	51	3	Good	Double	2022-01-01 09:20:00+00:00	kWh	Diff	30min
51	FCX.FCN2.PM01002_01_SUM.SUM	52	3	Good	Double	2022-01-01 09:20:00+00:00	kWh	Diff	30min
75	FCX.FCN2.PM01001_01_SUM.SUM	76	3	Good	Double	2022-01-01 09:30:00+00:00	kWh	Diff	30min
76	FCX.FCN2.PM01002_01_SUM.SUM	77	3	Good	Double	2022-01-01 09:30:00+00:00	kWh	Diff	30min
100	FCX.FCN2.PM01001_01_SUM.SUM	101	3	Good	Double	2022-01-01 09:40:00+00:00	kWh	Diff	30min
101	FCX.FCN2.PM01002_01_SUM.SUM	102	3	Good	Double	2022-01-01 09:40:00+00:00	kWh	Diff	30min
125	FCX.FCN2.PM01001_01_SUM.SUM	126	3	Good	Double	2022-01-01 09:50:00+00:00	kWh	Diff	30min
126	FCX.FCN2.PM01002_01_SUM.SUM	127	3	Good	Double	2022-01-01 09:50:00+00:00	kWh	Diff	30min
150	FCX.FCN2.PM01001_01_SUM.SUM	151	3	Good	Double	2022-01-01 10:00:00+00:00	kWh	Diff	30min
151	FCX.FCN2.PM01002_01_SUM.SUM	152	3	Good	Double	2022-01-01 10:00:00+00:00	kWh	Diff	30min
175	FCX.FCN2.PM01001_01_SUM.SUM	176	3	Good	Double	2022-01-01 10:10:00+00:00	kWh	Diff	30min
176	FCX.FCN2.PM01002_01_SUM.SUM	177	3	Good	Double	2022-01-01 10:10:00+00:00	kWh	Diff	30min
200	FCX.FCN2.PM01001_01_SUM.SUM	201	3	Good	Double	2022-01-01 10:20:00+00:00	kWh	Diff	30min
201	FCX.FCN2.PM01002_01_SUM.SUM	202	3	Good	Double	2022-01-01 10:20:00+00:00	kWh	Diff	30min
225	FCX.FCN2.PM01001_01_SUM.SUM	226	3	Good	Double	2022-01-01 10:30:00+00:00	kWh	Diff	30min
226	FCX.FCN2.PM01002_01_SUM.SUM	227	3	Good	Double	2022-01-01 10:30:00+00:00	kWh	Diff	30min
250	FCX.FCN2.PM01001_01_SUM.SUM	251	3	Good	Double	2022-01-01 10:40:00+00:00	kWh	Diff	30min
251	FCX.FCN2.PM01002_01_SUM.SUM	252	3	Good	Double	2022-01-01 10:40:00+00:00	kWh	Diff	30min
275	FCX.FCN2.PM01001_01_SUM.SUM	276	3	Good	Double	2022-01-01 10:50:00+00:00	kWh	Diff	30min
276	FCX.FCN2.PM01002_01_SUM.SUM	277	3	Good	Double	2022-01-01 10:50:00+00:00	kWh	Diff	30min
300	FCX.FCN2.PM01001_01_SUM.SUM	301	3	Good	Double	2022-01-01 11:00:00+00:00	kWh	Diff	30min
301	FCX.FCN2.PM01002_01_SUM.SUM	302	3	Good	Double	2022-01-01 11:00:00+00:00	kWh	Diff	30min
325	FCX.FCN2.PM01001_01_SUM.SUM	326	3	Good	Double	2022-01-01 11:10:00+00:00	kWh	Diff	30min
326	FCX.FCN2.PM01002_01_SUM.SUM	327	3	Good	Double	2022-01-01 11:10:00+00:00	kWh	Diff	30min
350	FCX.FCN2.PM01001_01_SUM.SUM	351	3	Good	Double	2022-01-01 11:20:00+00:00	kWh	Diff	30min
351	FCX.FCN2.PM01002_01_SUM.SUM	352	3	Good	Double	2022-01-01 11:20:00+00:00	kWh	Diff	30min
375	FCX.FCN2.PM01001_01_SUM.SUM	376	3	Good	Double	2022-01-01 11:30:00+00:00	kWh	Diff	30min
376	FCX.FCN2.PM01002_01_SUM.SUM	377	3	Good	Double	2022-01-01 11:30:00+00:00	kWh	Diff	30min
400	FCX.FCN2.PM01001_01_SUM.SUM	401	3	Good	Double	2022-01-01 11:40:00+00:00	kWh	Diff	30min
401	FCX.FCN2.PM01002_01_SUM.SUM	402	3	Good	Double	2022-01-01 11:40:00+00:00	kWh	Diff	30min
425	FCX.FCN2.PM01001_01_SUM.SUM	426	3	Good	Double	2022-01-01 11:50:00+00:00	kWh	Diff	30min
426	FCX.FCN2.PM01002_01_SUM.SUM	427	3	Good	Double	2022-01-01 11:50:00+00:00	kWh	Diff	30min
450	FCX.FCN2.PM01001_01_SUM.SUM	451	3	Good	Double	2022-01-01 12:00:00+00:00	kWh	Diff	30min
451	FCX.FCN2.PM01002_01_SUM.SUM	452	3	Good	Double	2022-01-01 12:00:00+00:00	kWh	Diff	30min
475	FCX.FCN2.PM01001_01_SUM.SUM	476	3	Good	Double	2022-01-01 12:10:00+00:00	kWh	Diff	30min
476	FCX.FCN2.PM01002_01_SUM.SUM	477	3	Good	Double	2022-01-01 12:10:00+00:00	kWh	Diff	30min
500	FCX.FCN2.PM01001_01_SUM.SUM	501	3	Good	Double	2022-01-01 12:20:00+00:00	kWh	Diff	30min
501	FCX.FCN2.PM01002_01_SUM.SUM	502	3	Good	Double	2022-01-01 12:20:00+00:00	kWh	Diff	30min
525	FCX.FCN2.PM01001_01_SUM.SUM	526	3	Good	Double	2022-01-01 12:30:00+00:00	kWh	Diff	30min
526	FCX.FCN2.PM01002_01_SUM.SUM	527	3	Good	Double	2022-01-01 12:30:00+00:00	kWh	Diff	30min
550	FCX.FCN2.PM01001_01_SUM.SUM	551	3	Good	Double	2022-01-01 12:40:00+00:00	kWh	Diff	30min
551	FCX.FCN2.PM01002_01_SUM.SUM	552	3	Good	Double	2022-01-01 12:40:00+00:00	kWh	Diff	30min
575	FCX.FCN2.PM01001_01_SUM.SUM	576	3	Good	Double	2022-01-01 12:50:00+00:00	kWh	Diff	30min
576	FCX.FCN2.PM01002_01_SUM.SUM	577	3	Good	Double	2022-01-01 12:50:00+00:00	kWh	Diff	30min
600	FCX.FCN2.PM01001_01_SUM.SUM	601	3	Good	Double	2022-01-01 13:00:00+00:00	kWh	Diff	30min
601	FCX.FCN2.PM01002_01_SUM.SUM	602	3	Good	Double	2022-01-01 13:00:00+00:00	kWh	Diff	30min

Expected output

	item	id	_value	agentstatus	type	agenttimestamp	Unit	Calc1	Calc2
75	FCX.FCN2.PM01001_01_SUM.SUM	76	3	Good	Double	2022-01-01 09:30:00+00:00	kWh	Diff	30min
76	FCX.FCN2.PM01002_01_SUM.SUM	77	3	Good	Double	2022-01-01 09:30:00+00:00	kWh	Diff	30min
150	FCX.FCN2.PM01001_01_SUM.SUM	151	3	Good	Double	2022-01-01 10:00:00+00:00	kWh	Diff	30min
151	FCX.FCN2.PM01002_01_SUM.SUM	152	3	Good	Double	2022-01-01 10:00:00+00:00	kWh	Diff	30min
225	FCX.FCN2.PM01001_01_SUM.SUM	226	3	Good	Double	2022-01-01 10:30:00+00:00	kWh	Diff	30min
226	FCX.FCN2.PM01002_01_SUM.SUM	227	3	Good	Double	2022-01-01 10:30:00+00:00	kWh	Diff	30min
300	FCX.FCN2.PM01001_01_SUM.SUM	301	3	Good	Double	2022-01-01 11:00:00+00:00	kWh	Diff	30min
301	FCX.FCN2.PM01002_01_SUM.SUM	302	3	Good	Double	2022-01-01 11:00:00+00:00	kWh	Diff	30min
375	FCX.FCN2.PM01001_01_SUM.SUM	376	3	Good	Double	2022-01-01 11:30:00+00:00	kWh	Diff	30min
376	FCX.FCN2.PM01002_01_SUM.SUM	377	3	Good	Double	2022-01-01 11:30:00+00:00	kWh	Diff	30min
450	FCX.FCN2.PM01001_01_SUM.SUM	451	3	Good	Double	2022-01-01 12:00:00+00:00	kWh	Diff	30min
451	FCX.FCN2.PM01002_01_SUM.SUM	452	3	Good	Double	2022-01-01 12:00:00+00:00	kWh	Diff	30min
525	FCX.FCN2.PM01001_01_SUM.SUM	526	3	Good	Double	2022-01-01 12:30:00+00:00	kWh	Diff	30min
526	FCX.FCN2.PM01002_01_SUM.SUM	527	3	Good	Double	2022-01-01 12:30:00+00:00	kWh	Diff	30min
600	FCX.FCN2.PM01001_01_SUM.SUM	601	3	Good	Double	2022-01-01 13:00:00+00:00	kWh	Diff	30min
601	FCX.FCN2.PM01002_01_SUM.SUM	602	3	Good	Double	2022-01-01 13:00:00+00:00	kWh	Diff	30min

What I tried and the actual output:

I tried with df.resample('30T', on='agenttimestamp') but it isn't working or the output isn't giving the results as expected, the output is same as the input when I apply resample.

Please let me know how to do this ?

答案1

得分: 1

关于 df[df['agenttimestamp'].dt.minute % 30 == 0] 的部分:

df[df['agenttimestamp'].dt.minute % 30 == 0]

更新:要删除第一个时间戳,假设第一个时间戳出现两次,您可以使用 iloc

df[df['agenttimestamp'].dt.minute % 30 == 0].iloc[2:]
英文:

What about df[df['agenttimestamp'].dt.minute % 30 == 0]?

from io import StringIO
import pandas as pd

df = pd.read_csv(StringIO(
"""agenttimestamp
'2022-01-01 09:00:00+00:00'
'2022-01-01 09:00:00+00:00'
'2022-01-01 09:10:00+00:00'
'2022-01-01 09:10:00+00:00'
'2022-01-01 09:20:00+00:00'
'2022-01-01 09:20:00+00:00'
'2022-01-01 09:30:00+00:00'
'2022-01-01 09:30:00+00:00'
'2022-01-01 09:40:00+00:00'
'2022-01-01 09:40:00+00:00'
'2022-01-01 09:50:00+00:00'
'2022-01-01 09:50:00+00:00'
'2022-01-01 10:00:00+00:00'
'2022-01-01 10:00:00+00:00'
'2022-01-01 10:10:00+00:00'
'2022-01-01 10:10:00+00:00'
'2022-01-01 10:20:00+00:00'
'2022-01-01 10:20:00+00:00'
'2022-01-01 10:30:00+00:00'
'2022-01-01 10:30:00+00:00'
'2022-01-01 10:40:00+00:00'
'2022-01-01 10:40:00+00:00'
'2022-01-01 10:50:00+00:00'
'2022-01-01 10:50:00+00:00'
'2022-01-01 11:00:00+00:00'
'2022-01-01 11:00:00+00:00'
'2022-01-01 11:10:00+00:00'
'2022-01-01 11:10:00+00:00'
'2022-01-01 11:20:00+00:00'
'2022-01-01 11:20:00+00:00'
'2022-01-01 11:30:00+00:00'
'2022-01-01 11:30:00+00:00'
'2022-01-01 11:40:00+00:00'
'2022-01-01 11:40:00+00:00'
'2022-01-01 11:50:00+00:00'
'2022-01-01 11:50:00+00:00'
'2022-01-01 12:00:00+00:00'
'2022-01-01 12:00:00+00:00'
'2022-01-01 12:10:00+00:00'
'2022-01-01 12:10:00+00:00'
'2022-01-01 12:20:00+00:00'
'2022-01-01 12:20:00+00:00'
'2022-01-01 12:30:00+00:00'
'2022-01-01 12:30:00+00:00'
'2022-01-01 12:40:00+00:00'
'2022-01-01 12:40:00+00:00'
'2022-01-01 12:50:00+00:00'
'2022-01-01 12:50:00+00:00'
'2022-01-01 13:00:00+00:00'
'2022-01-01 13:00:00+00:00'"""), parse_dates=['agenttimestamp'])

df[df['agenttimestamp'].dt.minute % 30 == 0]

# Output:
0  2022-01-01 09:00:00+00:00
1  2022-01-01 09:00:00+00:00
6  2022-01-01 09:30:00+00:00
7  2022-01-01 09:30:00+00:00
12 2022-01-01 10:00:00+00:00
13 2022-01-01 10:00:00+00:00
18 2022-01-01 10:30:00+00:00
19 2022-01-01 10:30:00+00:00
24 2022-01-01 11:00:00+00:00
25 2022-01-01 11:00:00+00:00
30 2022-01-01 11:30:00+00:00
31 2022-01-01 11:30:00+00:00
36 2022-01-01 12:00:00+00:00
37 2022-01-01 12:00:00+00:00
42 2022-01-01 12:30:00+00:00
43 2022-01-01 12:30:00+00:00
48 2022-01-01 13:00:00+00:00
49 2022-01-01 13:00:00+00:00

Update: to remove the first timestamp, assuming the first timestamp appears twice, you can use iloc:

df[df['agenttimestamp'].dt.minute % 30 == 0].iloc[2:]

huangapple
  • 本文由 发表于 2023年8月10日 20:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定