gpt4 book ai didi

python - Pandas - 重采样和标准差

转载 作者:行者123 更新时间:2023-12-02 15:33:48 28 4
gpt4 key购买 nike

我有这个数据框:

startTime     endTime  emails_received
index
2014-01-24 14:00:00 1390568400 1390569600 684
2014-01-24 14:00:00 1390568400 1390569300 700
2014-01-24 14:05:00 1390568700 1390569300 438
2014-01-24 14:05:00 1390568700 1390569900 586
2014-01-24 16:00:00 1390575600 1390576500 752
2014-01-24 16:00:00 1390575600 1390576500 743
2014-01-24 16:00:00 1390575600 1390576500 672
2014-01-24 16:00:00 1390575600 1390576200 712
2014-01-24 16:00:00 1390575600 1390576800 708

我运行 resample("10min",how="median").dropna() 并得到:

                  startTime     endTime  emails_received
start
2014-01-24 14:00:00 1390568550 1390569450 635
2014-01-24 16:00:00 1390575600 1390576500 712

这是正确的。有什么方法可以通过 pandas 轻松获得平均值的标准差吗?

最佳答案

您只需要在您的 DataFrame 上调用 .std()。这是一个说明性示例。

创建一个DatetimeIndex

In [38]: index = pd.DatetimeIndex(start='2000-1-1',freq='1T', periods=1000)

创建一个包含 2 列的 DataFrame

In [45]: df = pd.DataFrame({'a':range(1000), 'b':range(1000,3000,2)}, index=index)

DataFrame 的 Head、Std 和 Mean

In [47]: df.head()Out[47]:                      a     b2000-01-01 00:00:00  0  10002000-01-01 00:01:00  1  10022000-01-01 00:02:00  2  10042000-01-01 00:03:00  3  10062000-01-01 00:04:00  4  1008In [48]: df.std()Out[48]: a    288.819436b    577.638872dtype: float64In [49]: df.mean()Out[49]: a     499.5b    1999.0dtype: float64

下采样并执行计算相同的统计分数

In [54]: df = df.resample(rule="10T",how="median")In [55]: dfOut[55]: DatetimeIndex: 100 entries, 2000-01-01 00:00:00 to 2000-01-01 16:30:00Freq: 10TData columns (total 2 columns):a    100  non-null valuesb    100  non-null valuesdtypes: float64(1), int64(1)In [56]: df.head()Out[56]:                         a     b2000-01-01 00:00:00   4.5  10092000-01-01 00:10:00  14.5  10292000-01-01 00:20:00  24.5  10492000-01-01 00:30:00  34.5  10692000-01-01 00:40:00  44.5  1089In [57]: df.std()Out[57]: a    290.11492b    580.22984dtype: float64In [58]: df.mean()Out[58]: a     499.5b    1999.0dtype: float64

通过std()进行下采样

In [62]: df2 = df.resample(rule="10T", how=np.std)In [63]: df2Out[63]: DatetimeIndex: 100 entries, 2000-01-01 00:00:00 to 2000-01-01 16:30:00Freq: 10TData columns (total 2 columns):a    100  non-null valuesb    100  non-null valuesdtypes: float64(2)In [64]: df2.head()Out[64]:                            a         b2000-01-01 00:00:00  3.02765  6.0553012000-01-01 00:10:00  3.02765  6.0553012000-01-01 00:20:00  3.02765  6.0553012000-01-01 00:30:00  3.02765  6.0553012000-01-01 00:40:00  3.02765  6.055301

以下是 .std() 方法的文档字符串中的信息。

Return standard deviation over requested axis.NA/null values are excludedParameters----------axis : {0, 1}    0 for row-wise, 1 for column-wiseskipna : boolean, default True    Exclude NA/null values. If an entire row/column is NA, the result    will be NAlevel : int, default None    If the axis is a MultiIndex (hierarchical), count along a    particular level, collapsing into a DataFrameReturns-------std : Series (or DataFrame if level specified)        Normalized by N-1 (unbiased estimator).

关于python - Pandas - 重采样和标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21480041/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com