'Counting values greater than 0 in a given area (specific Rows * Columns) - Python, Excel, Pandas
Based on the following data:
| Participant | Condition | RT |
|---|---|---|
| 1 | 1 | 0.10 |
| 1 | 1 | |
| 1 | 2 | 0.48 |
| 2 | 1 | 1.2 |
| 2 | 2 | |
| 2 | 2 | 0.58 |
What is the appropriate code to count the values which are greater than 0 based on the term:
Participant == 1 and Condition == 1
The answer Should be: N = 1 noting that an empty slot is not considered
Looking forward to your reply with gratitude, Avishai
Solution 1:[1]
Use a boolean mask and sum it:
N = sum((df['Participant'] == 1) & (df['Condition'] == 1) & (df['RT'].notna()))
print(N)
# Output
1
Details:
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Corralien |
