使用Pandas Python计算每日气候 [英] Compute daily climatology using pandas python

查看:139
本文介绍了使用Pandas Python计算每日气候的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用熊猫来计算日常气候.我的代码是:

I am trying to use pandas to compute daily climatology. My code is:

import pandas as pd

dates      = pd.date_range('1950-01-01', '1953-12-31', freq='D')
rand_data  = [int(1000*random.random()) for i in xrange(len(dates))]
cum_data   = pd.Series(rand_data, index=dates)
cum_data.to_csv('test.csv', sep="\t")

cum_data是包含1950年1月1日至1953年12月31日的每日日期的数据框.我想创建一个长度为365的新矢量,其中第一个元素包含1950、1951、1952和1953年1月1日的rand_data平均值.以此类推,第二个元素...

cum_data is the data frame containing daily dates from 1st Jan 1950 to 31st Dec 1953. I want to create a new vector of length 365 with the first element containing the average of rand_data for January 1st for 1950, 1951, 1952 and 1953. And so on for the second element...

有人建议我如何使用熊猫吗?

Any suggestions how I can do this using pandas?

推荐答案

您可以按年份分组,然后计算这些组的平均值:

You can groupby the day of the year, and the calculate the mean for these groups:

cum_data.groupby(cum_data.index.dayofyear).mean()

但是,您要注意leap年.这将导致这种方法出现问题.另外,您还可以按月和日分组:

However, you have the be aware of leap years. This will cause problems with this approach. As alternative, you can also group by the month and the day:

In [13]: cum_data.groupby([cum_data.index.month, cum_data.index.day]).mean()
Out[13]:
1  1     462.25
   2     631.00
   3     615.50
   4     496.00
...
12  28    378.25
    29    427.75
    30    528.50
    31    678.50
Length: 366, dtype: float64

这篇关于使用Pandas Python计算每日气候的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆