Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用csv文件,查找平均温度_Python_Python 3.x - Fatal编程技术网

Python 使用csv文件,查找平均温度

Python 使用csv文件,查找平均温度,python,python-3.x,Python,Python 3.x,我需要一些帮助。所以我有一个 (+8785行) 日期/时间、温度(C)、露点温度(C)、相对湿度(%)、风速(km/h)、能见度(km)、Stn压力(kPa)、天气 2012-01-01 00:00:00,-1.8,-3.9,86,4,8.0101.24,雾 2012-01-01 01:00:00,-1.8,-3.7,87,4,8.0101.24,雾 2012-01-01 02:00:00,-1.8,-3.4,89,7,4.0101.26,“冻雨,雾” 2012-01-01 03:00:00,-

我需要一些帮助。所以我有一个 (+8785行)

日期/时间、温度(C)、露点温度(C)、相对湿度(%)、风速(km/h)、能见度(km)、Stn压力(kPa)、天气
2012-01-01 00:00:00,-1.8,-3.9,86,4,8.0101.24,雾
2012-01-01 01:00:00,-1.8,-3.7,87,4,8.0101.24,雾
2012-01-01 02:00:00,-1.8,-3.4,89,7,4.0101.26,“冻雨,雾”
2012-01-01 03:00:00,-1.5,-3.2,88,6,4.0101.27,“冻雨,雾”
2012-01-01 04:00:00,-1.5,-3.3,88,7,4.8101.23,雾
2012-01-01 05:00:00,-1.4,-3.3,87,9,6.4101.27,雾
2012-01-01 06:00:00,-1.5,-3.1,89,7,6.4101.29,雾
2012-01-01 07:00:00,-1.4,-3.6,85,7,8.0101.26,雾
2012-01-01 08:00:00,-1.4,-3.6,85,9,8.0101.23,雾
2012-01-01 09:00:00,-1.3,-3.1,88,15,4.0101.2,雾
2012-01-01 10:00:00,-1.0,-2.3,91,9,1.2101.15,雾
2012-01-01 11:00:00,-0.5,-2.1,89,7,4.0100.98,雾
2012-01-01 12:00:00,-0.2,-2.0,88,9,4.8100.79,雾
2012-01-01 13:00:00,0.2,-1.7,87,13,4.8100.58,雾
2012-01-01 14:00:00,0.8,-1.1,87,20,4.8100.31,雾
2012-01-01 15:00:00,1.8,-0.4,85,22,6.4100.07,雾
2012-01-01 16:00:00,2.6,-0.2,82,13,12.9,99.93,多云
2012-01-01 17:00:00,3.0,0.0,81,13,16.1,99.81,多云
2012-01-01 18:00:00,3.8,1.0,82,15,12.9,99.74,雨
所以,我基本上需要的是得到每个温度的平均值。例如(输出):

天气平均温度
清除6.825716
多云7.970544
毛毛雨7.353659
毛毛雨、雾8.067500
毛毛雨、冰粒、雾0.400000
毛毛雨,雪1050000
毛毛雨、雪、雾0.693333
雾4.303333
冻雨-5.657143
冻雨、雾-2.533333
冻雨、薄雾-5.433333
........
我所拥有的:

import csv
weather_file = csv.DictReader(open("weather_2012.csv", 'r'), 
                              delimiter=',', quotechar='"')

results = {}

for row in weather_file:

    weather = row["Weather"].split(" "" ")
    if not (weather in results):
        results[weather] = {
            "max": float(row["Temp (C)"])
        }
        continue

    if float(row["Temp (C)"]) > results[weather]["max"]:
        results[weather]["max"] = float(row["Temp (C)"])

y=[]
print("Weather   Mean Temperature")
for month in sorted(results, key=lambda results: results):
    y.append(results[month]["max"])

    print("%s %.1f" % (weather[month], results[month]["max"]))
我必须找到某个温度的平均值和它的意义

一定的天气条件有一定的温度。我必须根据天气条件定义(分类)所有温度。例如:

“多云”天气条件已超过+300。我必须找到它的平均温度,并定义为“多云”天气


这里有一种方法:

#!/usr/bin/env python3
import csv
from pprint import pprint

filename = 'weather_2012.csv'
condition_mean_temps = {}

# Initially associate a list of temperature values with each condition.
with open(filename, 'r', newline='') as csvfile:
    reader = csv.reader(csvfile); next(reader)  # skip header row
    # Only need second and last value from each row of csv data file.
    for _, temperature, *_, condition in reader:
        condition_mean_temps.setdefault(condition, []).append(float(temperature))

# (Re)associate the mean of the associated list of values with each condition.
condition_mean_temps = {condition: round(sum(temperatures)/len(temperatures), 2)
                            for condition, temperatures
                                in condition_mean_temps.items()}

pprint(condition_mean_temps)
输出:

{'Clear':6.83,
“多云”:7.97,
"毛毛雨":7.35,,
“毛毛雨,雾”:8.07,
“毛毛雨、冰粒、雾”:0.4,
“毛毛雨,雪”:1.05,
“毛毛雨、雪、雾”:0.69,
“雾”:4.3,
“冻雨”:-5.66,
“冻雨,雾”:-2.53,
“冰冷的毛毛雨,薄雾”:-5.43,
“冰冷的毛毛雨,雪”:-5.11,
‘冰雾’:-7.58,
“冻雨”:-3.89,
“冻雨,雾”:-2.22,
“冻雨,阴霾”:-4.9,
“冻雨、冰粒、雾”:-2.6,
“冻雨,雪粒”:-5.0,
“烟雾”:-0.2,
“主要清晰”:12.56,
“中雨,雾”:1.7,
“中雪”:-5.53,
“中雪,吹雪”:-5.45,
“多云”:10点57分,
"雨":9.79,,
“阵雨”:13.72,
“雨阵雨,雾”:12.8,
“雨阵雨,雪阵雨”:2.15,
“雨,雾”:8.27,
“雨,雾”:4.63,
“雨,冰粒”:0.6,
“雨,雪”:1.06,
“雨,雪粒”:1.9,
“雨、雪、雾”:0.8,
“雨、雪、冰粒”:1.1,
“雪”:-4.52,
“雪球”:0.7,
“雪阵雨”:-3.51,
“雪阵雨,雾”:-10.68,
“雪,吹雪”:-5.41,
“雪,雾”:-5.08,
“雪,雾”:-4.02,
“雪,冰粒”:-1.88,
"雷暴":24时15分,
“雷雨、暴雨”:10.9,
“雷雨、中雨、雾”:19.6,
“雷雨,雨”:20.43,
“雷雨,阵雨”:20.04,
“雷雨、阵雨、雾”:21.6,
“雷雨、雨、雾”:20.6}

这里有一种使用熊猫的方法

import numpy as np
import pandas as pd

d = pd.read_csv("test.csv")
means = d.groupby('Weather')['Temp (C)'].mean()
print means
我假设数据存储在
test.csv
文件中

pandas是一个数据分析库,它有三个基本概念系列,DataFrame和Panel。这里我们创建一个数据帧。可以将此视为数据的列行表示形式。这正是csv所做的。因此,与熊猫一起使用csv非常容易

要了解更多信息,请查看以下内容-


此特定解决方案可在此处找到-

请显示CSV文件的一些行您有问题吗?那代码是做什么的?你希望它做什么呢?提出具体问题的建议<代码>熊猫可能比您正在尝试的要容易得多。你只需要根据天气分组,然后找出平均值。哎呀,你的问题看起来很奇怪地类似于这个问题:-这是你作业的一部分吗?@martineau这可能是他的作业的一部分,也可能不是他的作业的一部分,但该用户显然使用了几个帐户…最后一部分可以通过听写理解完成,也许你可以解释更多关于跳过中间部分的内容。否则这是个好办法。@Jean-Françoisfare:谢谢。我不确定你解释“中间部分的跳过”是什么意思,但我用你建议的词典替换了最后一部分(这并不重要)。我指的是
*.
部分,它消耗除解包中最后一项之外的所有项。@Jean-Françoisfare:星号是Python 3中引入的漂亮的新语法。正式名称为ExtendedIterable解包(又名“*目标特性”),在中有很好的描述。我知道的唯一书面解释是在文档中,它被称为“带星号前缀的目标”。