如何每隔一定时间计算最大最小值,变量的平均值和标准偏差 [英] How I can calculate the max min value, the mean and standard deviation of a variable every certain time

查看:139
本文介绍了如何每隔一定时间计算最大最小值,变量的平均值和标准偏差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有30天的温度数据和其他5个变量,我已经将它们拆分并每天获取为变量(第1天,第2天,第3天,.....,第30天),这是工作的第一部分是分别绘制图表,然后将其叠加在图表中30天,然后查看行为模式,然后在第二部分中,我需要计算最大值和最小值,温度变量的平均值和标准偏差,每10分钟计算一次每天,每天获取约144个值.我在以下链接中使用.txt数据: mega.nz/#!cDQGCISJ!LUD9JhJIOVGyhmS8iyHQo6x9V9KWUdfR0_T882PAv0c 每天看起来像这样:

I have the data of 30 days of Temperature and other 5 variables, i already split them and get every day as a variable(Day1 ,Day2 ,Day3 ,.....,Day30),the FIRST PART of the work is plot the graphs individually and then overlay in a graph the 30 days and see a pattern of behavior, then in the SECOND PART I need to calculate the max and min value, the mean and standard deviation of the Temperature variable every 10 minutes for each day, get aprox 144 values for every day. Im working with the .txt data in the following link: mega.nz/#!cDQGCISJ!LUD9JhJIOVGyhmS8iyHQo6x9V9KWUdfR0_T882PAv0c every day looks like this:

在此处输入图片描述

我需要计算(T的最小值和最大值,每10分钟一次的均值和标准差,直到一天结束,然后使其他29天都这样),我希望Python计算分钟数并进行以下操作:

And I need to calculate the (Min and Max Value of T, the mean and Standard Deviation every 10 minutes until the day finish and make it like that for the other 29 days) I want Python count the minutes and make this:

在此处输入图片描述

到目前为止,我只有第一部分的代码如下(分别绘制图形和30天的叠加图的代码),我需要帮助:

and the code i have so far is the following with only the first part(the code for plot the graph individually and the overlay plot with the 30 days) i need help for the :

import pandas as pd
from datetime import date
import datetime as dt
import calendar
import numpy as np
import pylab as plt 
import matplotlib.ticker as ticker
import seaborn as sns

datos = pd.read_csv("Jun2019.txt", sep = ',', names=('Fecha', 'Hora', 'RADNETA', 'RADCORENT', 'RADCORSAL', 'RADINFENT', 'RADINFSAL', 'TEMP'))

datos['Hora'] = datos['Hora'].str[:9] **#Use this part to get rid of the miliseconds(mm.xxxx)**
datos['Hora']

Dia01Jun2019 = datos[datos['Fecha'] == "2019-06-01"] 

tiempo01=Dia01Jun2019['Hora']
temp01=Dia01Jun2019['TEMP']

imagen = plt.figure(figsize=(25,10))
plt.plot(tiempo01,temp01)
plt.xticks(np.arange(0, 54977, 7000)) 
plt.xlabel("Tiempo (H:M:S)(Formato 24 Horas)")
plt.ylabel("Temperatura (K)")
plt.title("Día 01 Jun 2019")
plt.show()
imagen.savefig('D1JUN2019')

每天重复执行一次代码,只是因为我没有在这里放置太久,然后覆盖的部分才是(向Mig B社区用户提供了以下代码,对我有帮助):

The code repeats for every day just that i dont put here cause is to long and then the overlay part is(credits to Mig B community user that helped me with the following code):

imagen = plt.figure(figsize=(25,10))

for day in [1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30]:
    dia = datos[datos['Fecha'] == "2019-06-"+(f"{day:02d}")]
    tiempo= pd.to_datetime(dia['Hora'], format=' %H:%M:%S').dt.time
    temp= dia['TEMP']
    plt.plot(tiempo, temp) #, color = 'red' )# 

plt.xlabel("Tiempo (H:M:S)(Formato 24 Horas)")
plt.ylabel("Temperatura (K)")
plt.title("Temperatura Jun 2019")
plt.show()
imagen.savefig('TEMPJUN2019')

这是到目前为止的代码,我没有计算第二部分的想法,我感谢制作第二部分的所有帮助,记录循环for day in [1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30]:,我省略了几天,因为它们存在测量问题.

And this is the code so far , i dont have and idea to calculate the SECOND PART, I appreciate every help to make the second part, for the record the loop for day in [1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30]: I omit some days because they had measurement problems.

某些先前的价值信息可能会在我在此处提出的先前的问题中找到

Some previous value information maybe can be found in a previous question that i made here How to make overlay plots of a variable, but every plot than i want to make has a different length of data

也有人在这里问类似的内容,是否可以作为指南的帮助

Also someone ask something similar in here if that help as a guide Group the values for a certain time interval and calculate the means Just that he had data every 10 minutes and want the mean every 3 hours get 8 values for day , And in my case I have the data every aprox 2 minutes , want the mean every 10 minutes and get aprox 144 values for day.

推荐答案

df.iloc[[1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30],:].describe()

索引[p,1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30,

for index [1,2,3,4,5,6,8,11,12,13,14,15,16,17,18,19,20,23,26,27,28,30].

要将日期设置为索引:

df = df.set_index('column_name_of_your_day')

如果要每5行切片:

for i in range(0,len(df),5):
    df.iloc[i:i+5,:].describe()

这篇关于如何每隔一定时间计算最大最小值,变量的平均值和标准偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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