【问题标题】:How to get average/mean of column where night = 1 for example例如,如何获得夜间 = 1 的列的平均值/平均值
【发布时间】:2020-10-11 12:32:44
【问题描述】:

谁能帮我获得每晚的平均温度,例如温度?因此,我可以将其与另一个数据帧进行比较,在该数据帧中我可以看到某人的睡眠时间。

图片是数据框的外观。

【问题讨论】:

  • 请在您的问题中发布您的数据框,而不是作为图片。

标签: python pandas numpy mean


【解决方案1】:

指定晚上的时间。将它们用于日出和日落。 有一个函数time_to_float(time) 来计算自午夜以来的秒数。

如果是晚上,则将温度加到总和中,并将温度数加一。

计算温度的总和并除以温度的数量。

with open('data.txt' , 'r') as file:
   data= file.read()
   temp_sum=0
   number_temp=0
   for line in data:
       line=line.strip().split(',') #strip the end and split at separator e.g. ,
       time = (line[2].split()[1] # get time without date
       time = time_to_float(time) #write a function to calculate time since 00:00:00
       if (time > sunset or time< sunrise):
           temp-sum+=float(line[3])
           number_temp+=1
   temp_mean=temp_sum/number_temp

【讨论】:

    【解决方案2】:

    您可以发布数据框以便更好地理解。使用熊猫,您可以设置一个索引,然后按“夜”列进行分组并找到 表示温度列的聚合平均值。

    df.groupby('Night')['Temperature'].mean()

    【讨论】:

      猜你喜欢
      • 2012-03-18
      • 2022-08-14
      • 2015-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多