I Have one for condition, and can't do if statement to count cell condition
Question:
I’m trying to do this condition to count how many times (cells) have values less than 300 seconds (5 minutes) but it returns me an error, the condition is about a DF below, that I pull from my csv:
df =
0
0 NaN
1 79.0
2 140.0
3 131.0
4 72.0
... ...
16341 349.0
16342 795.0
16343 787.0
16344 410.0
16345 1221.0
I try this way:
for key, value in data_df.items() :
df = data_df[key].fillna(0, inplace=False).astype(int)
if key <= 300 :
df_count = value.count( value.values() <= 300)
display(df_count)
but the error message says:
TypeError: ‘numpy.ndarray’ object is not callable
How can I solve my IF
statement to execute the Display()
code?
EDITED FOR BETTER RESULTS
I Should asked this before but i thought that the sollution would be alike IF
statement.
I need to make it in range, the condition is to count every cell in a range about 5 to 5 minutes, untill I get 1 hour complete, So the conditions is about:
- 0 to 300 (5minutes)
- 300 to 600 (5 to 10 minutes)
- 600 to 900 (10 minutes to 15 minutes)
.
.
.
until i get one hour complete, I tryed:
df_count300to600 = data_df[key].fillna(0).le( key > 300 && key< 600 )
How can I solve at this point?
Answers:
IIUC, you just need:
count = df[0].fillna(0).le(300).sum()
Example output: 5
How it works
df[0].fillna(0).le(300)
creates a boolean Series, sum
counts the True
:
0 df['0'].fillna(0).le(300)
0 NaN True
1 79.0 True
2 140.0 True
3 131.0 True
4 72.0 True
16341 349.0 False
16342 795.0 False
16343 787.0 False
16344 410.0 False
16345 1221.0 False
I’m trying to do this condition to count how many times (cells) have values less than 300 seconds (5 minutes) but it returns me an error, the condition is about a DF below, that I pull from my csv:
df =
0
0 NaN
1 79.0
2 140.0
3 131.0
4 72.0
... ...
16341 349.0
16342 795.0
16343 787.0
16344 410.0
16345 1221.0
I try this way:
for key, value in data_df.items() :
df = data_df[key].fillna(0, inplace=False).astype(int)
if key <= 300 :
df_count = value.count( value.values() <= 300)
display(df_count)
but the error message says:
TypeError: ‘numpy.ndarray’ object is not callable
How can I solve my IF
statement to execute the Display()
code?
EDITED FOR BETTER RESULTS
I Should asked this before but i thought that the sollution would be alike IF
statement.
I need to make it in range, the condition is to count every cell in a range about 5 to 5 minutes, untill I get 1 hour complete, So the conditions is about:
- 0 to 300 (5minutes)
- 300 to 600 (5 to 10 minutes)
- 600 to 900 (10 minutes to 15 minutes)
.
.
.
until i get one hour complete, I tryed:
df_count300to600 = data_df[key].fillna(0).le( key > 300 && key< 600 )
How can I solve at this point?
IIUC, you just need:
count = df[0].fillna(0).le(300).sum()
Example output: 5
How it works
df[0].fillna(0).le(300)
creates a boolean Series, sum
counts the True
:
0 df['0'].fillna(0).le(300)
0 NaN True
1 79.0 True
2 140.0 True
3 131.0 True
4 72.0 True
16341 349.0 False
16342 795.0 False
16343 787.0 False
16344 410.0 False
16345 1221.0 False