将DataFrameGroupBy拆分为单个帧(Pandas)

2024-09-29 03:33:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我按hostoperation列对以下DF进行了分组:

df
Out[163]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 100 entries, 10069 to 1003
Data columns (total 8 columns):
args             100  non-null values
host             100  non-null values
kwargs           100  non-null values
log_timestamp    100  non-null values
operation        100  non-null values
thingy             100  non-null values
status           100  non-null values
time             100  non-null values
dtypes: float64(1), int64(2), object(5)


g = df.groupby(['host','operation'])

g
Out[165]: <pandas.core.groupby.DataFrameGroupBy object at 0x7f46ec731890>

g.groups.keys()[:10]
Out[166]:
[('yy39.segm1.org', 'gtfull'),
 ('yy39.segm1.org', 'updateWidg'),
 ('yy36.segm1.org', 'notifyTestsDelivered'),
 ('yy32.segm1.org', 'notifyTestsDelivered'),
 ('yy20.segm1.org', 'gSettings'),
 ('yy32.segm1.org', 'x_gWidgboxParams'),
 ('yy39.segm1.org', 'clearElems'),
 ('yy3.segm1.org', 'gxyzinf'),
 ('yy34.segm1.org', 'setFlagsOneWidg'),
 ('yy13.segm1.org', 'x_gbinf')]

现在我需要为每个('host','operation')对获取单独的数据帧。我可以通过迭代组键来实现:

^{pr2}$

问题:

问题1。我想知道是应该拆分DataFrameGroupBy对象,还是有更快的方法来实现目标?在

战略上:我需要计算指数加权移动平均值和指数加权标准差(尽管std-dev应该衰减得慢得多)。在

为此,我需要:

a.按主机、操作分组

b.按日志时间戳排序的每个主机/操作子集

c.计算time列的ewma和ewstd。在

有没有一种方法可以在不分裂DataFrameGroupBy的情况下实现这一目标?在

问题2。目标是在主机/操作的特定时间在过去几分钟内出现异常(过载情况)时发出信号。我有一个想法,如果我计算出“慢ewmsd”和“slow ewma”(较长时间,比如1小时),那么短期ewma(比如5分钟)可以解释为紧急值,如果它与慢速ewma(三西格玛规则)的偏差超过2个。我甚至不确定这是不是正确的/最好的方法。它是?在

可能是这样,因为这与unix1m、5m和15m的平均负载工作方式大致相似:如果15m是正常的,但是1m的平均负载要高得多,那么您知道负载比通常要高得多。但我不确定。在


Tags: 方法orgcorehostpandasdfoutoperation