计算给定区域(特定行 * 列)中大于 0 的值 - Python、Excel、Pandas

Counting values greater than 0 in a given area (specific Rows * Columns) - Python, Excel, Pandas

基于以下数据:

Participant Condition RT
1 1 0.10
1 1
1 2 0.48
2 1 1.2
2 2
2 2 0.58

根据术语计算大于 0 的值的适当代码是什么:

Participant == 1 and Condition == 1

答案应该是:N = 1 注意不考虑空槽

期待您的回复 带着感激之情, 阿维沙伊

使用布尔掩码并求和:

N = sum((df['Participant'] == 1) & (df['Condition'] == 1) & (df['RT'].notna()))
print(N)

# Output
1

详情:

m1 = df['Participant'] == 1
m2 = df['Condition'] == 1
m3 = df['RT'].notna()
df[['m1', 'm2', 'm3']] = pd.concat([m1, m2, m3], axis=1)
print(df)

# Output
   Participant  Condition    RT     m1     m2     m3
0            1          1  0.10   True   True   True  # All True, N = 1
1            1          1   NaN   True   True  False
2            1          2  0.48   True  False   True
3            2          1  1.20  False   True   True
4            2          2   NaN  False  False  False
5            2          2  0.58  False  False   True