【问题标题】:Aggregation on Pandas data frame for selected rows对选定行的 Pandas 数据框进行聚合
【发布时间】:2016-09-10 06:45:42
【问题描述】:

我有一个像这样的pandas 排序数据框(基于时间):

from datetime import datetime
df = pd.DataFrame({ 'ActivityDateTime' : [datetime(2016,5,13,6,14),datetime(2016,5,13,6,16),
                                 datetime(2016,5,13,6,20),datetime(2016,5,13,6,27),datetime(2016,5,13,6,31),
                                 datetime(2016,5,13,6,32),
                                datetime(2016,5,13,17,34),datetime(2016,5,13,17,36),
                                 datetime(2016,5,13,17,38),datetime(2016,5,13,17,45),datetime(2016,5,13,17,47),
                                datetime(2016,5,16,13,3),datetime(2016,5,16,13,6),
                                 datetime(2016,5,16,13,10),datetime(2016,5,16,13,14),datetime(2016,5,16,13,16)],
              'Value1' : [0.0,2.0,3.0,4.0,0.0,0.0,0.0,7.0,8.0,4.0,0.0,0.0,3.0,9.0,1.0,0.0],
               'Value2' : [0.0,2.0,3.0,4.0,0.0,0.0,0.0,7.0,8.0,4.0,0.0,0.0,3.0,9.0,1.0,0.0]
        })

结果是这样的:

ActivityDateTime    Value1  Value2
0   2016-05-13 06:14:00 0.0 0.0
1   2016-05-13 06:16:00 2.0 2.0
2   2016-05-13 06:20:00 3.0 3.0
3   2016-05-13 06:27:00 4.0 4.0
4   2016-05-13 06:31:00 0.0 0.0
5   2016-05-13 06:32:00 0.0 0.0
6   2016-05-13 17:34:00 0.0 0.0
7   2016-05-13 17:36:00 7.0 7.0
8   2016-05-13 17:38:00 8.0 8.0
9   2016-05-13 17:45:00 4.0 4.0
10  2016-05-13 17:47:00 0.0 0.0
11  2016-05-16 13:03:00 0.0 0.0
12  2016-05-16 13:06:00 3.0 3.0
13  2016-05-16 13:10:00 9.0 9.0
14  2016-05-16 13:14:00 1.0 1.0
15  2016-05-16 13:16:00 0.0 0.0

我想在没有 for 循环的情况下聚合数据(平均)。但是,我将观察结果分组的方式并不简单!查看Value1,我想将它们组合为non-zero 值。例如,索引1,2,3 将在一个组中。 Incidies 7,8,9 在一组中,另一组是12,13,14。应避免使用value1==0 所在的行,而零仅充当组之间的分隔符。最终我想得到这样的东西:

Activity_end    Activity_start  Value1  Value2  num_observations
0   2016-05-13 06:27:00 2016-05-13 06:16:00 4.50    4.50    3
1   2016-05-13 17:45:00 2016-05-13 17:36:00 6.33    6.33    3
2   2016-05-16 13:14:00 2016-05-16 13:06:00 4.33    4.33    3

目前,我在想我应该以某种方式将数字 123 分配给一个新列,然后基于此聚合它们。我不确定如何在没有 for 循环的情况下制作该列!请注意Value1Value2 不一定相同。

【问题讨论】:

  • 他们总是三人一组吗?如果是这样,那么您可以删除所有零行,然后以 3 为一组。
  • @Jezzamon 不幸的是没有:/他们可以在一个组中的任何数量。

标签: python pandas time-series aggregate


【解决方案1】:

一种方法是创建一些临时列

# First create a new series, which is true whenever the value changes from a zero value to a non-zero value (which will be at the start of each group)
nonzero = (df['Value1'] > 0) & (df['Value1'].shift(1) == 0)
# Take a cumulative sum. This means each group will have it's own number.
df['group'] = df['nonzero'].cumsum()
# Group by the group column
gb = df[df['Value1'] > 0].groupby('group')

然后您可以使用聚合函数 http://pandas.pydata.org/pandas-docs/stable/groupby.html 对该组进行聚合

对于您特别希望获得的输出,也请查看以下答案:Python Pandas: Multiple aggregations of the same column

df2 = gb.agg({
    'ActivityDateTime': ['first', 'last'],
    'Value1': 'mean',
    'Value2': 'mean'})

【讨论】:

  • 太棒了。这完全有效。我认为gb = df[df['Value'] > 0].groupby('group') 中有错字,应该是Value1
  • 一个后续问题。想象一下,我的列中有nan 值。基于 link ,我正在使用 np.nanmean'np.nanmean' ,但它们都不起作用。 np.nanmean(np.array[1,2,np.nan]) 虽然完全可以在我的电脑上运行。
  • 没关系 :) 使用from functools import partial,然后定义s_na_mean = partial(pd.Series.mean, skipna = True),我使用它而不是np.nanmean,它可以工作。
猜你喜欢
  • 2018-12-01
  • 2021-06-03
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2016-05-01
  • 2017-05-07
  • 2013-06-05
相关资源
最近更新 更多