简体   繁体   English

有没有办法在 dataframe 中仅使用大于 0 的列中的值来计算平均值?

[英]Is there a way to calculate average with only values in the columns greater than 0 in dataframe?

Is there a way to calculate average with only values in the columns greater than 0 in dataframe?有没有办法在 dataframe 中仅使用大于 0 的列中的值来计算平均值? I have a dataset with the structure below.我有一个结构如下的数据集。

Week    |Mon    |Tues   |Weds      |Thurs  |Fri    |Sat     |Sun    |average
:-------:-------:-------:----------:-------:-------:--------:-------:-----------
1       |5915   |2997   |8499      |1248   |3089   |1343    |0      |3298.71
2       |0      |0      |3588      |5693   |1297   |288     |2453   |1902.71

Using mean will get all the values in the stated columns which gives Total/7.使用平均值将获得所述列中的所有值,即 Total/7。 df['average'] = df[['Mon', 'Tues','Weds','Thurs','Fri','Sat','Sun']].mean(axis=1)

Is there a way to get results in with the following:有没有办法通过以下方式获得结果:

Week    |Mon    |Tues   |Weds      |Thurs  |Fri    |Sat     |Sun    |average (desired output)
:-------:-------:-------:----------:-------:-------:--------:-------:--------------------------------
1       |5915   |2997   |8499      |1248   |3089   |1343    |0      |(5915+2997+8499+1248+3089+1343)/6
2       |0      |0      |3588      |5693   |1297   |288     |2453   |(3588+5693+1297+288+2453)/5

Just add another layer of boolean indexing to make it only positive numbers:只需添加另一层 boolean 索引,使其只有正数:

df[df[['Mon', 'Tues','Weds','Thurs','Fri','Sat','Sun']]>0].mean(axis=1)

暂无
暂无

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

相关问题 过滤数据框列值大于零? - Filter dataframe columns values greater than zero? 选择DataFrame行,其中列大于Series中的值 - Select DataFrame rows where columns are greater than values in a Series 在 Pandas 中:如何检查列表元素是否大于数据框列值 - In Pandas : How to check a list elements is Greater than a Dataframe Columns Values 查找多列中的值是否大于 pandas Dataframe 中的常量 - Finding if values in multiple columns greater than constant in pandas Dataframe 如何从pandas数据框中选择平均值大于某些限制的列? - How do I select columns from pandas dataframe that have an average value greater than some limit? 列表中大于平均值的数值 - Number values in a list greater than the average 如何使用一个数据帧中的值来计算大于或小于第二数据帧中的值的总数? - How can the values from one dataframe be used to calculate the total number of values that are greater than or lesser than it in a second dataframe? 如何在 dataframe 中选择两行,计算每列中两个值的平均值并将新行与 dataframe 中的平均值相加 - How to choose two rows in a dataframe, calculate the average of both values in each columns and add the new row with the averages in the dataframe 按其他 dataframe 对 dataframe 的列进行分组并计算聚合列的加权平均值 - Grouping columns of dataframe by other dataframe and calculate weighted average of aggregated columns 获取数据帧中行的平均值大于或等于零 - Getting average of rows in dataframe greater than or equal to zero
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM