pandas Groupby回报平均值但!排除NaN [英] Pandas Groupby Return Average BUT! exclude NaN
问题描述
因此,我试图利用pandas groupby函数并减少我拥有的大数据框.这是一个示例:
So Im trying to make sense of the pandas groupby function and to reduce a large data frame I have. Here is an example:
A B
2016-09-23 19:36:08+00:00 NaN 34.0
2016-09-23 19:36:11+00:00 NaN 33.0
2016-09-23 19:36:12+00:00 24.1 NaN
2016-09-23 19:36:14+00:00 NaN 34.0
2016-09-23 19:36:17+00:00 NaN 34.0
2016-09-23 19:36:20+00:00 NaN 34.0
2016-09-23 19:36:22+00:00 24.2 NaN
2016-09-23 19:36:23+00:00 NaN 34.0
2016-09-23 19:36:26+00:00 NaN 34.0
2016-09-23 19:36:29+00:00 NaN 34.0
2016-09-23 19:36:32+00:00 24.1 NaN
2016-09-23 19:36:33+00:00 NaN 34.0
2016-09-23 19:37:00+00:00 NaN 34.0
2016-09-23 19:37:02+00:00 24.1 NaN
因此,我有2个数据系列"A"和"B",它们以不同的采样率以其采样时间作为原始数据帧的索引进行采样.
So I have 2 data series "A" and "B" that were sampled at different rates with their sampling time as the index of the original data frame.
我现在想按日期/小时/分钟对数据框的行进行分组,并返回每分钟数据的平均值.在这里,平均值应该忽略数据框中的缺失值.
I would like to now group the rows of the data frame by date/hour/minute and return the average of the data per minute. Here the average should ignore the missing values in the data frame.
例如,我将返回如下内容:
So for example, I would return something like this:
A B
2016-09-23 19:36:00+00:00 24 34.0
2016-09-23 19:37:00+00:00 24.1 33.0
是否可以使用内置的熊猫函数来做到这一点?
Is it possible to do this with a built in pandas function?
推荐答案
我认为您需要 Resampler.mean
,用于计算组的均值,不包括缺失值:
I think you need resample
with Resampler.mean
, which compute mean of groups, excluding missing values:
print (df.resample('1Min').mean())
A B
2016-09-23 19:36:00 24.133333 33.888889
2016-09-23 19:37:00 24.100000 34.000000
使用groupby
的另一种解决方案:
Another solution with groupby
:
print (df.groupby([pd.TimeGrouper('1Min')]).mean())
A B
2016-09-23 19:36:00 24.133333 33.888889
2016-09-23 19:37:00 24.100000 34.000000
这篇关于 pandas Groupby回报平均值但!排除NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!