Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The answer Should be: N = 1 noting that an empty slot is not considered

Looking forward to your reply
with gratitude,
Avishai

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use