python - 使用 csv 文件,查找温度平均值

标签 python python-3.x

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

Date/Time,Temp (C),Dew Point Temp (C),Rel Hum (%),Wind Spd (km/h),Visibility (km),Stn Press (kPa),Weather
2012-01-01 00:00:00,-1.8,-3.9,86,4,8.0,101.24,Fog
2012-01-01 01:00:00,-1.8,-3.7,87,4,8.0,101.24,Fog
2012-01-01 02:00:00,-1.8,-3.4,89,7,4.0,101.26,"Freezing Drizzle,Fog"
2012-01-01 03:00:00,-1.5,-3.2,88,6,4.0,101.27,"Freezing Drizzle,Fog"
2012-01-01 04:00:00,-1.5,-3.3,88,7,4.8,101.23,Fog
2012-01-01 05:00:00,-1.4,-3.3,87,9,6.4,101.27,Fog
2012-01-01 06:00:00,-1.5,-3.1,89,7,6.4,101.29,Fog
2012-01-01 07:00:00,-1.4,-3.6,85,7,8.0,101.26,Fog
2012-01-01 08:00:00,-1.4,-3.6,85,9,8.0,101.23,Fog
2012-01-01 09:00:00,-1.3,-3.1,88,15,4.0,101.2,Fog
2012-01-01 10:00:00,-1.0,-2.3,91,9,1.2,101.15,Fog
2012-01-01 11:00:00,-0.5,-2.1,89,7,4.0,100.98,Fog
2012-01-01 12:00:00,-0.2,-2.0,88,9,4.8,100.79,Fog
2012-01-01 13:00:00,0.2,-1.7,87,13,4.8,100.58,Fog
2012-01-01 14:00:00,0.8,-1.1,87,20,4.8,100.31,Fog
2012-01-01 15:00:00,1.8,-0.4,85,22,6.4,100.07,Fog
2012-01-01 16:00:00,2.6,-0.2,82,13,12.9,99.93,Mostly Cloudy
2012-01-01 17:00:00,3.0,0.0,81,13,16.1,99.81,Cloudy
2012-01-01 18:00:00,3.8,1.0,82,15,12.9,99.74,Rain

所以,我基本上需要的是了解每个温度。例如(输出):

Weather Mean Temperature
Clear 6.825716
Cloudy 7.970544
Drizzle 7.353659
Drizzle,Fog 8.067500
Drizzle,Ice Pellets,Fog 0.400000
Drizzle,Snow 1.050000
Drizzle,Snow,Fog 0.693333
Fog 4.303333
Freezing Drizzle -5.657143
Freezing Drizzle,Fog -2.533333
Freezing Drizzle,Haze -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"]))

我必须找到特定温度的平均值及其含义......

特定的天气条件具有特定的温度。我必须根据天气条件定义(排序)所有温度。例如:

"Cloudy" weather condition have been more than +300. I have to find average of its Temperature and define as "Cloudy" weather.

最佳答案

这是使用 Pandas 执行此操作的一种方法

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是一个数据分析库,它具有三个基本概念Series、DataFrame和Panel。这里我们正在创建一个数据框。您可以将其视为数据的列行表示。这正是 csv 的作用。因此,使用 pandas 处理 csv 非常容易。

要了解更多信息,请查看 - http://pandas.pydata.org/

这个具体的解决方案可以在这里找到 - http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html

关于python - 使用 csv 文件,查找温度平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41211710/

相关文章:

python - 如何使用正则表达式从文本中构建 python 列表?

python - 如何在 while 循环中使用索引每次查找下一个出现的位置

python - Scipy 或 bayesian 优化函数与 Python 中的约束、边界和数据框

python - Unicode 文件名到 python subprocess.call()

python - 如何更改 JSON 中包含引号的文本?

python - 根据类别过滤帖子

python - 将字符串每一行中的第一个单词存储到列表中

python - 当用C扩展Python时,如何用C动态构建复杂的结构?

python - Counter() 和字符串格式化

Python if/elif 问题与 random.randint