python - 根据选定的窗口进行数据帧聚合

标签 python pandas dataframe

我正在使用 python 和 panda 数据框。 我有一个从 CSV 文件导入的数据框。

         volume  temperature(c)
time(sec)
1000.1  10.4   26.5
1000.2  12.5   30.2
1000.3  13.2   40.5
.
.
.
8000.1  78   50.8
8000.2  79   51.5

我想创建一个新的数据框,我们定义一个时间窗口 W(例如 5 秒),并且每 W 秒将使用特定窗口上的不同计算将每列的值聚合到一行,例如、平均值、标准 z 分数等。 输出数据帧示例:

time(sec) mean_volume mean_temperature std_volume
1000.1  12.0.  32.4 1.4
1005.1  12.5   30.2 1.7
1010.1  11.7   30.1 1.5
.
.
.

我熟悉df['new col'] = data['source'].rolling(W).mean(),这不是我的情况的解决方案 我附上了示例

    T,H,L,C,label
1000.1,23.18,27.272,426,1
1000.2,23.15,27.2675,429.5,1
1000.3,23.15,27.245,426,1
1000.4,23.15,27.2,426,1
1000.5,23.1,27.2,426,1
1000.6,23.1,27.2,419,1
1000.7,23.1,27.2,419,1
1000.8,23.1,27.2,419,1
1000.9,23.1,27.2,419,1
1001,23.075,27.175,419,1
1001.1,23.075,27.15,419,1
1001.2,23.1,27.1,419,1
1001.3,23.1,27.16666667,419,1
1001.4,23.05,27.15,419,1
1001.5,23,27.125,419,1
1001.6,23,27.125,418.5,1
1001.7,23,27.2,0,0
1001.8,22.945,27.29,0,0
1001.9,22.945,27.39,0,0
1002,22.89,27.39,0,0
1002.1,22.89,27.39,0,0
1002.2,22.89,27.39,0,0
1002.3,22.89,27.445,0,0

对于上面的示例,我希望新的数据帧将包含以下列:H_mean、H_std、L_mean、C_mean、L_std、C_std

此外,我如何在每个分割上应用自定义函数(例如 z 分数)。

谢谢

最佳答案

鉴于您的数据位于名为 dfpd.DataFrame 中,以下操作应该可以解决问题:

import pandas as pd
import numpy as np
step = 5
df.groupby(pd.cut(df.index,
                 np.arange(start=df.index.min(), stop=df.index.max(), step=step, 
                 dtype=float)))\
           .agg({'volume':['mean', 'std'], 'temperature':['mean']})

我们正在使用 pd.cut 创建一个 IntervalIndex,我们可以对其进行groupby。最后我们使用 pd.DataFrame.agg 来计算每个组的汇总统计数据; volume 列的 meanstdtemperary 列的 mean

我还没有测试过这个,但是如果你提供 minimal, complete and verifiable example ,我能做到。

编辑

鉴于更新的数据,我编写了以下代码:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: from io import StringIO

In [4]: s = """T,H,L,C,label
   ...: 1000.1,23.18,27.272,426,1
   ...: 1000.2,23.15,27.2675,429.5,1
   ...: 1000.3,23.15,27.245,426,1
   ...: 1000.4,23.15,27.2,426,1
   ...: 1000.5,23.1,27.2,426,1
   ...: 1000.6,23.1,27.2,419,1
   ...: 1000.7,23.1,27.2,419,1
   ...: 1000.8,23.1,27.2,419,1
   ...: 1000.9,23.1,27.2,419,1
   ...: 1001,23.075,27.175,419,1
   ...: 1001.1,23.075,27.15,419,1
   ...: 1001.2,23.1,27.1,419,1
   ...: 1001.3,23.1,27.16666667,419,1
   ...: 1001.4,23.05,27.15,419,1
   ...: 1001.5,23,27.125,419,1
   ...: 1001.6,23,27.125,418.5,1
   ...: 1001.7,23,27.2,0,0
   ...: 1001.8,22.945,27.29,0,0
   ...: 1001.9,22.945,27.39,0,0
   ...: 1002,22.89,27.39,0,0
   ...: 1002.1,22.89,27.39,0,0
   ...: 1002.2,22.89,27.39,0,0
   ...: 1002.3,22.89,27.445,0,0"""

In [5]: df = pd.read_csv(StringIO(s), index_col='T')

我们再次使用 IntervalIndexgroupby 以及 agg 来计算汇总统计数据。

In [6]: step = 0.5
    ...: 
    ...: grouped = df.groupby(pd.cut(df.index,
    ...:                  np.arange(start=df.index.min(), stop=df.index.max(), step=step, dtype=float
    ...: )))
    ...: 

In [7]: grouped.agg({'H':['mean', 'std'], 'L':['mean', 'std'], 'C':['mean', 'std']})
Out[7]: 
                       H                    L                C          
                    mean       std       mean       std   mean       std
(1000.1, 1000.6]  23.130  0.027386  27.222500  0.031820  425.3  3.834058
(1000.6, 1001.1]  23.090  0.013693  27.185000  0.022361  419.0  0.000000
(1001.1, 1001.6]  23.050  0.050000  27.133333  0.025685  418.9  0.223607
(1001.6, 1002.1]  22.934  0.046016  27.332000  0.085557    0.0  0.000000

这不会为您提供所需的列名称,因此让我们展平列 MultiIndex 来调整这些名称。

In [8]: aggregated = grouped.agg({'H':['mean', 'std'], 'L':['mean', 'std'], 'C':['mean', 'std']})

In [9]: ['_'.join(col).strip() for col in aggregated.columns.values]
Out[9]: ['H_mean', 'H_std', 'L_mean', 'L_std', 'C_mean', 'C_std']

In [10]: aggregated.columns = ['_'.join(col).strip() for col in aggregated.columns.values]

In [11]: aggregated
Out[11]: 
                  H_mean     H_std     L_mean     L_std  C_mean     C_std
(1000.1, 1000.6]  23.130  0.027386  27.222500  0.031820   425.3  3.834058
(1000.6, 1001.1]  23.090  0.013693  27.185000  0.022361   419.0  0.000000
(1001.1, 1001.6]  23.050  0.050000  27.133333  0.025685   418.9  0.223607
(1001.6, 1002.1]  22.934  0.046016  27.332000  0.085557     0.0  0.000000

我不太清楚应用 Z 分数是什么意思,因为这不是一个汇总统计数据,与 stdmean 不同,所以它不会播放与聚合很好。如果您只想按列将 Z 分数应用于整个 DataFrame,我建议您可能想看看这个问题:Pandas - Compute z-score for all columns

关于python - 根据选定的窗口进行数据帧聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51410508/

相关文章:

python - 从多个 Excel 文件和工作表中具有相同名称的特定列中提取数据

python - 如何将DataFrame列收集到键值对中作为python中的行

python - 如何以编程方式确定(在 Python 中)何时有人通过 RDP 连接到我的 Windows 7 计算机?

python - 从列中的字符串中删除不需要的部分

python - 找不到 antlr-python-runtime 的匹配分布

python - 在 python pandas 中使用两个不同的数据集生成散点图

python - 使用 numpy 数组中的值从 DataFrame 创建 Pandas DataFrame 以访问数据帧索引

python - 根据字典检查数据帧值(作为键,值元组)的矢量化方法?

c# - 从 C# 调用 Python

Python 'continue' 干扰 'try/except' 或 'with'?