简体   繁体   English

Pandas 列的条件平均值,其中值大于或小于零

[英]Pandas conditional mean of column where values greater than or less than zero

I want to calculate the conditional mean of a column: If the values of the row elements are >0 then calculate mean of all such elements and if <0 then calculate the mean of these and store in avgGain and avgLoss.我想计算列的条件平均值:如果行元素的值 >0,则计算所有这些元素的平均值,如果 <0,则计算这些元素的平均值并存储在 avgGain 和 avgLoss 中。

Input:输入:

ProfitLoss
    -8.000
    14.400
    13.150
     3.050
    -8.000
    -8.000
     3.425
     7.350
    -8.000
    -8.000
     0.000

Output:输出:

avgGain     avgLoss
 8.275      -8.000

All these calculations should happen using either pandas apply or aggregate functions in a single statement.所有这些计算都应该在单个语句中使用 pandas apply 或聚合函数进行。

Thanks谢谢

IIUC, could do: IIUC,可以这样做:

# Setup (for reproducibility)
import pandas as pd

data = [-8.000,
    14.400,
    13.150,
     3.050,
    -8.000,
    -8.000,
     3.425,
     7.350,
    -8.000,
    -8.000,
     0.000]
df = pd.DataFrame(data, columns=["ProfitLoss"])

# Calculate the respective means (vectorized)
avgGain = df[df['ProfitLoss'] > 0].mean().values[0]
avgLoss = df[df['ProfitLoss'] < 0].mean().values[0]

# Print outputs to console
print("avgGain:", avgGain)
print("avgLoss:", avgLoss)

outputs:输出:

Matthews-MacBook-Pro:stackoverflow matt$ python test.py
avgGain: 8.275
avgLoss: -8.0

as desired如预期的

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

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