1

我有一个文件“tancoyol.csv”,其中包含每 15 分钟记录的 Fecha、Temperatura、Humedad、PreciAcu 数据。它具有以下形式:

Fecha DirViento MagViento Temperatura HUmedad PreciAcu
2011-07-01 00:00:00 318 6.6 21.22 100 1.7
2011-07-01 00:15:00 342 5.5 21.20 100 1.7
2011-07-01 00:320:06 4.8
2011-07-01 00:45:00 279 7.5 21.11 100 4.2
2011-07-01 01:00:00 318 6.0 21.16 100 2.5 2011-07-01
01:15:00 329 7.4.1 21.13 1-10-20
01 01:30:00 300 4.7 21.15 100 1.3
2011-07-01 01:45:00 291 3.1 21.23 100 2.2
2011-07-01 02:00:00 284 7.6 21.29 100 1.3
2011-07-01 02:15:00 315 0.0 21.43 100
01-7 02:30:00 281 3.6 21.47 100 3.2
2011-07-01 02:45:00 0 2.7 21.52 100 2.5
2011-07-01 03:00:00 57 1.2 21.53 100 0.0
20151-07-01 03: 300 3.4 21.69 100 0.0
2011-07-01 03:30:00 359 5.9 21.67 100 0.0
2011-07-01 03:45:00 309 1.8 21.65 100 0.0
2011-07-01 04:00:00 244 13.4 21.64 100 0.0
2011-07-02 00:00:00 312 6.0 23.05 97 0.0
2011-07-02 00:15:00 318 6.3 22.79 100 02-3
02-3 00:30:00 303 9.1 22.44 100 0.7
2011-07-02 00:45:00 323 6.3 22.40 100 0.3
2011-07-02 01:00:00 319 5.4 22.07 100 0.7
2011-07-02: 4 3.9 21.89 100 0.8
2011-07-02 01:30:00 6 4.5 21.74 100 0.7
2011-07-02 01:45:00 310 5.0 21.72 100 1.3
2011-07-02 02:00:00 307 0.0 21.79 100 1.0
2011-07-02 02:15:00 5 3.4 21.78 100 1.2 2011-07-02
02:30:00 288 3.4 21.71-07 1.5 0-2
02:45:00 0 2.6 21.66 100 1.5
2011-07-02 03:00:00 280 5.8 21.48 100 1.3
2011-07-02 03:15:00 29 0.0 21.43 100 1.5
20101-07-02 03: 332 2.0 21.23 100 1.7
2011-07-02 03:45:00 148 0.0 21.06 100 1.5
2011-07-02 04:00:00 132 0.0 21.00 100 2.0
2011-07-03 00:00:00 308 8.0 21.93 99 0.3 2011-07-03
00:15:00 288 14.4 21.85 99 0.2
2011-07-03 00:30:00 354 3.1 21.85 99 03.3 20-
00:45:00 335 5.8 21.75 100 0.2
2011-07-03 01:00:00 274 2.7 21.68 100 0.0
2011-07-03 01:15:00 328 5.6 21.55 100 0.3
2011-07-03: 319 7.9 21.38 100 0.2
2011-07-03 01:45:00 322 5.1 21.32 100 0.3
2011-07-03 02:00:00 317 2.8 21.21 100 0.2
2011-07-03 02:15:00 322 5.3 21.08 100 0.3 2011-07-03
02:30:00 291 4.3 21.06 100 0.2
2011-07-03 02:45:00 284 5.7 21.014 100 0-3-7
03:00:00 310 2.7 21.05 100 0.2
2011-07-03 03:15:00 318 4.6 21.06 100 0.3
2011-07-03 03:30:00 299 7.4 21.05 100 0.2
2011-07-03: 238 0.0 20.99 100 0.3
2011-07-03 04:00:00 310 1.4 21.05 100 0.2

我要做的第一件事是获取 DirViento、MagViento、Temperatura 和 Humedad 列的平均值。我这样做如下:

import pandas as pd import numpy as np

df = pd.read_csv('tancoyol.csv', parse_dates=[['Fecha','Hora']]) df1=df.set_index('Fecha_Hora') prom_diario=df1.resample('D',how=np.mean) print prom_diario

Fecha DirViento MagViento Temperatura Humedad PreciAcu

2011-07-01 318.000000 6.600000 21.220000 100.000000 1.700000
2011-07-02 273.470588 5.064706 21.474706 99.823529 1.688235
2011-07-03 200.705882 3.864706 21.775882 99.941176 1.076471
2011-07-04 306.812500 4.925000 21.310625 99.875000 0.231250

因为第 1 天、第 2 天和第 3 天的平均值没有完成,因为输出是滞后的,即第 2 天的平均值应该对应于第一天,依此类推。如何解决这个问题?现在,我不想获取 PreciAcu 列中的平均值,而是只获取 PreciAcu 列的每日总和,我该怎么做?最后,如何将输出(平均值和总和)存储到 csv 文件

我将非常感谢您的帮助

4

2 回答 2

0

我认为您正在寻找closed='right'和的label='right'论点resample

In [38]: hows = {'PreciAcu': 'sum'}

In [39]: func_keys = df.columns - Index(hows.keys())

In [40]: mean_funcs = zip(func_keys, ['mean'] * len(func_keys))

In [41]: hows.update(mean_funcs)

In [42]: hows
Out[42]:
{'DirViento': 'mean',
 'HUmedad': 'mean',
 'MagViento': 'mean',
 'PreciAcu': 'sum',
 'Temperatura': 'mean'}

In [48]: df.resample('D', how=hows, closed='right', label='right')
Out[48]:
            PreciAcu  HUmedad  Temperatura  DirViento  MagViento
ts
2011-07-01       1.7  100.000       21.220    318.000      6.600
2011-07-02      28.7   99.824       21.475    273.471      5.065
2011-07-03      18.3   99.941       21.776    200.706      3.865
2011-07-04       3.7   99.875       21.311    306.812      4.925

当然,正如@Dan Allan 所说,用于to_csv将新重新采样DataFrame的文件写入文件。

于 2013-10-10T01:05:28.660 回答
0

要对一列求和并平均其他列,请传递列名和函数的字典。

In [47]: df.resample('D', {'DirViento': np.mean, 'MagViento': np.mean, 'Temperatura': np.mean, 'HUmedad': np.mean, 'PreciAcu': np.sum})
Out[47]: 
            PreciAcu  Temperatura     HUmedad   DirViento  MagViento
 0_1                                                                 
2011-07-01      30.4    21.367059  100.000000  273.823529   5.100000
2011-07-02      18.0    21.841765   99.823529  200.941176   3.747059
2011-07-03       4.0    21.347059   99.823529  306.882353   5.105882

对于输出滞后的原因,我不遵循您的推理,但是您可以像这样实现它:

In [53]: resampled = df.resample('D', {'DirViento': np.mean, 'MagViento': np.mean, 'Temperatura': np.mean, 'HUmedad': np.mean, 'PreciAcu': np.sum})

In [54]: resampled.tshift(-1)
Out[54]: 
            PreciAcu  Temperatura     HUmedad   DirViento  MagViento
0_1                                                                 
2011-06-30      30.4    21.367059  100.000000  273.823529   5.100000
2011-07-01      18.0    21.841765   99.823529  200.941176   3.747059
2011-07-02       4.0    21.347059   99.823529  306.882353   5.105882

将其保存为 CSV 非常简单:df1.to_csv('filename.csv').

于 2013-10-10T00:13:53.190 回答