简体   繁体   English

如何根据特定列中的值对数据帧的行求平均?

[英]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.

相关问题 熊猫数据框找到具有特定列值的所有行? - pandas data frame find all rows with particular column value? 在python中,如何从任何列中存在特定字符串的数据框中获取行(字符串值) - In python,how to get the rows from a data frame where a particular string is present in any of the column (String value) 根据特定数据框列处理异常 - handing exceptions based on a particular data frame column 如何找到数据帧特定列值的不同计数 - How to find distinct count of data frame particular column value 如何使用 pandas 根据某个列中的值合并/划分数据框中的行? - How to consolidate/divide rows within a data frame based on a value within a certain column using pandas? 如何根据数据框中的其他列值更改列值? - how to change the column value based on other column value in data frame? 如何从包含特定列中的特定字符串(多个)的 Pandas 数据框中删除行? - How to drop rows from pandas data frame that contains a particular string(multiple) in a particular column? 如何根据其他行值添加 pandas 数据框列 - How to add pandas data frame column based on other rows values 根据值从 pandas 数据帧中的特定位置获取最后 N 行 - Get last N rows from a particular place in pandas data frame based on a value 如何计算基于另一列的数据框列中值的频率? - How to count frequency of a value in a column of a data frame based on another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM