我有一个包含时间和值的矩阵/数据框:
# time # Value
M = [[2018-08-08 12:00:00, 5],
[2018-08-08 12:00:00, 7],
[2018-08-08 13:00:00, 2],]
我想按小时分组,然后计算组的平均值,然后修改/减少每个组,使其仅具有<=该平均值的值。
当前版本:
grouped = M.groupby(pd.Grouper(key='time', freq='1h'))
means = grouped['value'].mean().values # np.array([6, 2])
在这里我被卡住了。我得到每个组的平均值。但是我不知道如何减少“分组”,以便该条件对该组应用grouped [grouped ['value'] <= mean]。
赞赏任何建议。
预期输出:
N = [[2018-08-08 12:00:00, 5], # as 5 <= 6 where 6 is the mean of the first group
[2018-08-08 13:00:00, 2]] # as 2 is <= 2 where 2 is the mean of the second group
答案 0 :(得分:4)
将GroupBy.transform
用于Series
,其大小与原始DataFrame
相同,并用汇总值填充,因此boolean indexing
的工作情况非常好:
M = [['2018-08-08 12:00:00', 5],
['2018-08-08 12:00:00', 7],
['2018-08-08 13:00:00', 2]]
M = pd.DataFrame(M, columns=['time','value'])
M['time'] = pd.to_datetime(M['time'])
print (M)
time value
0 2018-08-08 12:00:00 5
1 2018-08-08 12:00:00 7
2 2018-08-08 13:00:00 2
s = M.groupby(pd.Grouper(key='time', freq='1h'))['value'].transform('mean')
print (s)
0 6
1 6
2 2
Name: value, dtype: int64
mean = 5
df = M[s <= mean]
print (df)
time value
2 2018-08-08 13:00:00 2
编辑:
您还可以按列值进行比较:
df1 = M[M['value'] <= s]
print (df1)
time value
0 2018-08-08 12:00:00 5
2 2018-08-08 13:00:00 2