[英]How to average rows of a data frame based the value in a particular column?
My dataframe has a column named "Zeroing" which is either True or False, and it alternates. 我的数据框有一个名为“ Zeroing”的列,该列为True或False,并且交替显示。 It's False for 12 rows then True for 48 rows.
对于12行为False,然后对于48行为True。 I'd like to make a new data frame where it's False for 1 row and True for 4, with all other columns averaged accordingly.
我想制作一个新的数据框,其中第一行为False,第四行为True,所有其他列均进行平均。 I know for sure that it's a 12/48 pattern, but would like to use the condition just to be certain.
我肯定知道这是一种12/48模式,但只是想确定使用该条件。
Assume for brevity that it alternates True for 2 rows then False for 8 and I want to average every 2 rows. 为简洁起见,假设它交替显示True为2行,然后为False为8,我想平均每2行。 I would have:
我会:
>>> df
Value1 Value2 Value3 Zeroing
0 1 2 0 True
1 2 4 5 True
2 3 6 10 False
3 4 8 15 False
4 5 10 20 False
5 6 12 25 False
6 7 14 30 False
7 8 16 35 False
8 9 18 40 False
9 10 20 45 False
I would like the output to be: 我希望输出为:
>>> df
Value1 Value2 Value3 Zeroing
0 1.5 3 2.5 True
1 3.5 7 12.5 False
2 5.5 11 22.5 False
3 7.5 15 32.5 False
4 9.5 19 42.5 False
For your example, you can use a groupby
and then take the mean for every two rows. 对于您的示例,您可以使用
groupby
,然后每两行取一次平均值。 Translate this to your actual problem. 将其转换为您的实际问题。
>>> df.groupby(df.index // 2).mean()
Which returns 哪个返回
Value1 Value2 Value3 Zeroing
0 1.5 3.0 2.5 True
1 3.5 7.0 12.5 False
2 5.5 11.0 22.5 False
3 7.5 15.0 32.5 False
4 9.5 19.0 42.5 False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.