如何在csv文件中找到前两周每天的平均温度



我是一名新的程序员,最近被引入pandas框架。我有一个大的csv文件,里面有几年来的日均温度,看起来像这样:

Station  Date    Tmax    Tmin    Tavg

1    5/1/2007    83  50  67
1    5/2/2007    59  42  51
2    5/2/2007    60  43  52
1    5/3/2007    66  46  56
2    5/3/2007    67  48  58
1    5/4/2007    66  49  58
2    5/4/2007    78  51  M
1    5/5/2007    66  53  60
2    5/5/2007    66  54  60
1    5/6/2007    68  49  59
2    5/6/2007    68  52  60

根据Tavg栏中的平均温度,我需要创建另一个栏,每天显示前两周的平均温度。

希望,我说清楚了。任何帮助都将不胜感激。

正如@MarkSetchell所建议的,有一个相关的问题特定列上熊猫的滚动平均值

简言之,Andrew L发布了的最佳答案

%timeit weather['ma'] = weather['Tavg'].rolling(5).mean()
%timeit weather['ma_2'] = weather.rolling(5).mean()['Tavg']
1000 loops, best of 3: 497 µs per loop
100 loops, best of 3: 2.6 ms per loop

除非需要将计算的滚动平均值存储在所有其他列上,否则不建议使用第二种方法。

最新更新