简体   繁体   English

熊猫Groupby返回平均值但! 排除NaN

[英]Pandas Groupby Return Average BUT! exclude NaN

So Im trying to make sense of the pandas groupby function and to reduce a large data frame I have. 因此,我试图利用pandas groupby函数并减少我拥有的大数据框。 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

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. 因此,我有两个数据系列“ A”和“ B”,它们以不同的采样率作为原始数据帧的索引以不同的速率采样。

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? 是否可以使用内置的熊猫函数来做到这一点?

I think you need resample with Resampler.mean , which compute mean of groups, excluding missing values: 我认为您需要使用Resampler.mean resample Resampler.mean ,该Resampler.mean将计算组的平均值,不包括缺失值:

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

Another solution with groupby : 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM