python - 将每日数据温度转换为 Pandas 的月份

标签 python python-2.7 numpy pandas matplotlib

我正在尝试使用 python 2.7 将 10 年(1991-2000)的每日数据温度转换为 pandas 中的月份。我已经从网页(“http://owww.met.hu/eghajlat/eghajlati_adatsorok/bp/Navig/201j_EN.htm”)中获取了数据。但是我遇到了麻烦。数据如下所示:

    ` datum  d_ta  d_tx  d_tn  d_rs d_rf  d_ss
---------- ----- ----- ----- ----- ---- -----
1991-01-01   3.0   5.4   1.5   0.2 1      0.0
1991-01-02   4.0   7.2   1.9   0.0 1      6.8
1991-01-03   6.0   8.8   3.6   0.0 1      2.5
1991-01-04   3.7   7.6   2.3    .         2.9
1991-01-05   4.9   7.2   1.5    .         0.0
1991-01-06   2.7   6.2   0.5    .         0.9
1991-01-07   4.0   8.4   1.9    .         3.2
1991-01-08   6.7   8.9   4.6   0.0 0      0.0
1991-01-09   4.1   8.0   3.0   0.3 0      0.0
1991-01-10   4.2   8.1   2.4   0.0 0      0.2
1991-01-11   4.7   6.9   3.6    .         0.7
1991-01-12   7.0   9.8   3.2    .         0.1
1991-01-13   6.3   8.2   4.6    .         0.0
1991-01-14   3.7   6.8   2.2    .         4.7
1991-01-15   0.7   3.4  -1.0    .         7.6
1991-01-16  -1.4   1.4  -3.0    .         7.5
1991-01-17  -2.5   2.1  -5.0    .         8.1
1991-01-18  -1.8   4.0  -5.1    .         7.0
1991-01-19  -3.0   0.1  -4.0    .         5.8
1991-01-20  -2.8   0.5  -5.2    .         5.6
1991-01-21  -5.0  -1.7  -7.8    .         0.0
1991-01-22  -3.3  -1.8  -4.2    .         0.0
1991-01-23  -1.7   0.4  -2.5    .         0.0
1991-01-24   0.0   3.2  -1.6    .         2.2
1991-01-25   1.1   5.1  -0.9    .         6.4
1991-01-26   0.6   4.5  -0.5    .         7.1
1991-01-27  -1.5   2.2  -4.0    .         0.0
1991-01-28   1.3   5.6  -0.8    .         3.8
1991-01-29   0.7   2.6  -0.4    .         1.1
1991-01-30   0.3   4.0  -1.2    .         7.3
1991-01-31  -5.0  -0.2  -7.4    .         8.0
1991-02-01  -8.1  -3.7 -11.7    .         7.6
1991-02-02  -7.0  -2.0 -10.2    .         7.4
1991-02-03  -5.3   0.8  -9.9    .         7.8
1991-02-04  -5.1  -2.3  -7.7   0.1 4      3.7
1991-02-05  -7.5  -4.4  -8.3    .         2.6
1991-02-06  -7.1  -2.2 -11.0   2.0 4      4.9
1991-02-07  -1.8   0.0  -2.7   2.7 4      0.0
1991-02-08  -1.8   0.4  -3.6  21.8 4      0.0
1991-02-09   0.8   2.0  -0.2   1.3 1      0.0
1991-02-10   1.6   3.4  -0.2   3.4 1      0.0
1991-02-11   0.7   2.5  -0.5   1.1 4      0.0
1991-02-12  -0.5   1.2  -1.0   4.7 4      0.0
1991-02-13  -2.0  -0.8  -2.6   0.0 4      0.0
1991-02-14  -1.8   1.4  -3.5   0.1 4      6.3
1991-02-15  -4.2  -0.8  -6.4    .         8.4
1991-02-16  -5.6  -2.4  -9.5   0.1 4      1.5
1991-02-17  -1.3   1.9  -3.8    .         8.3
1991-02-18  -1.3   4.5  -5.5    .         8.5
1991-02-19  -1.5   3.6  -4.7    .         5.8
1991-02-20  -1.4   4.7  -5.4    .         7.3
1991-02-21   1.0   6.1  -2.1    .         6.9
1991-02-22   4.1  10.1   0.5    .         3.2
1991-02-23   5.1   9.7   2.9    .         7.5
1991-02-24   6.0   8.6   5.5   0.0 1      1.8
1991-02-25   3.6   9.2   0.6    .         8.1
1991-02-26   3.9   9.3   1.2    .         2.9
1991-02-27   3.1   6.5   0.3    .         8.8
1991-02-28   1.4   5.3  -2.4    .         4.3
1991-03-01   1.7   3.5  -0.2    .         0.0
1991-03-02   2.4   3.3   1.7   0.8 4      0.0
1991-03-03   3.1   3.8   1.7    .         0.0
1991-03-04   4.3   6.2   2.7    .         1.5
1991-03-05   3.0   5.7   0.6    .         1.2
.........`

请有人帮助我如何将其转换为月份。谢谢!

最佳答案

从数字开始将表格复制到内存后:

import pandas, bs4, requests, itertools, io

html = requests.get("http://owww.met.hu/eghajlat/eghajlati_adatsorok/bp/Navig/201j_EN.htm").text
soup = bs4.BeautifulSoup(html)

# the manual way:
# data = pandas.read_clipboard(names=["datum", "d_ta", "d_tx", "d_tn", "d_rs", "d_rf", "d_ss"], index_col='datum', parse_dates=['datum'])

# the automatic way:
table_html = '\n'.join(itertools.islice(map(lambda _: _.text, soup.find_all("pre")), 3, None))
data = pandas.read_table(io.StringIO(table_html), header=None, sep='\s+', index_col=0, parse_dates=0,
                         names=["datum", "d_ta", "d_tx", "d_tn", "d_rs", "d_rf", "d_ss"])

data.resample('m').mean()

当然,您可以使用均值以外的其他聚合函数。输出:

            d_ta        d_tx        d_tn        d_rf        d_ss
datum                   
1991-01-31  1.345161    4.609677    -0.574194   3.000000    1.583333
1991-02-28  -1.142857   2.592857    -3.639286   5.157143    1.516667
1991-03-31  8.158065    12.093548   5.141935    2.645161    0.775000
1991-04-30  9.920000    14.570000   6.510000    4.066667    4.450000
1991-05-31  13.396774   17.780645   9.738710    4.529032    4.280000
...

关于python - 将每日数据温度转换为 Pandas 的月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36224733/

相关文章:

python - SleekXMPP 随意发送消息并仍然监听传入的消息。

python - Python 中从多个列表中仅选择一个唯一元素

python - 使用 py2 解释器时 PyCharm py3 语法问题

Python Numpy 二维数组迭代

python - 更改标签的文本颜色,按键时更改回来

python - 如何在 msys python 中获取 native Windows 路径?

python - 将 n 个任务添加到 celery 队列并等待结果

python - 在 python 和 c++ 之间管道传输二进制数据

python - 如何强制 scipy 使用 Adams 进行集成?

python - 如何根据上下粉色范围查找roi_corners,以便可以在python中使用opencv对其进行模糊处理