【发布时间】:2020-10-11 12:32:44
【问题描述】:
【问题讨论】:
-
请在您的问题中发布您的数据框,而不是作为图片。
【问题讨论】:
指定晚上的时间。将它们用于日出和日落。
有一个函数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
【讨论】:
您可以发布数据框以便更好地理解。使用熊猫,您可以设置一个索引,然后按“夜”列进行分组并找到 表示温度列的聚合平均值。
df.groupby('Night')['Temperature'].mean()
【讨论】: