【问题标题】:How I can calculate the max min value, the mean and standard deviation of a variable every certain time我如何计算每个特定时间变量的最大最小值、平均值和标准差
【发布时间】:2020-02-11 16:38:22
【问题描述】:

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

enter image description here

并且我需要计算(T 的最小值和最大值,每 10 分钟的平均值和标准偏差,直到一天结束,并在其他 29 天中保持这种状态)我希望 Python 计算分钟并进行此操作:

enter image description here

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

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 社区用户帮助我使用以下代码):

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]:我省略了一些日子,因为他们有测量问题。

一些以前的价值信息可能可以在我在这里提出的以前的问题中找到How to make overlay plots of a variable, but every plot than i want to make has a different length of data

还有人在这里问类似的问题,如果这有助于作为指导Group the values for a certain time interval and calculate the means 只是他每 10 分钟有一次数据,并且希望每 3 小时的平均值得到 8 个一天的值,而在我的情况下,我每大约 2 分钟就有一次数据,想要每 10 分钟的平均值并得到大约 144 个一天的值。 em>

【问题讨论】:

    标签: python python-3.x pandas loops statistics


    【解决方案1】:
    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()
    

    对于索引 [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()
    

    【讨论】:

    • 一旦我把这行放在上面并索引天数,我如何每 10 分钟计算一次这些值(最小值、最大值、平均值、标准差),在我的问题的恢复中,一旦我索引天数如何我可以告诉 python 访问一天并计算我需要每隔一段时间(在这种情况下为 10 分钟)的值,我修改帖子让你看看第 01 天的样子
    • 你应用固定窗口还是滚动窗口?例如,从早上 0 点到凌晨 1 点,您需要 6 个数据(每 10 分钟)还是希望 60 个数据每个都有您指定的移动窗口?对于第一个,您可以动态索引切片器,对于后者,您需要 rolling_window
    • 我不知道它是滚动窗口还是固定窗口,但我需要这个,例如,我有这个:00:00:00、00:00:02、00:00:05、00: 00:08, 00:00:0
    • 我不知道它是滚动窗口还是固定窗口,但我需要这个,例如,我有这 5 个第一行:00:01:00、00:02:00、00:05:05 , 00:08:08, 00:10:25。我需要这 5 条线的平均值作为一个值,然后对下一个值重复它 00:10:25, 00:12:00, 00:15:05, 00:18:08, 00:20:43 得到另一个平均值作为其他 5 行的一个值,关键是我每 10 分钟的数据需要一个 mean()
    • for i in range(0,len(df),5): df.iloc[i:i+5,:]
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 2021-09-23
    • 2019-12-11
    • 2016-05-10
    • 2014-11-20
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多