check and count a condition over all rows of a dataframe
Question:
I have a dataframe like this
data = { 'number':['one','two','three'],
'l_min_x':[6,6,6],
'l_max_x':[12,18,15],
'r_min_x':[6,6,6],
'r_max_x':[17,19,15]}
datadf= pd.DataFrame(data)
print(datadf)
and I get
number l_min_x l_max_x r_min_x r_max_x
0 one 6 12 6 17
1 two 6 18 6 19
2 three 6 15 6 15
and I want to check and count how many of the rows fulfill a condition.
Now I can do this going row by row like this question but I am wondering if there is another way that takes the whole dataframe and do the following:
-
Check how many rows have l_min_x
less than a value (for example 7) and l_max_x
greater than another value (say 17)
In this case the answer would be 1 (one)
-
Check how many rows have r_min_x
less than a value (for example 8) and r_max_x
greater than another value (say 16)
In this case the answer would be 2 (two)
Can I do this without iterating over all rows of the dataframe?
Answers:
I guess you could do this :
condition1= datadf.loc[(datadf["l_min_x"] < 7) & (datadf["l_max_x"] > 17) ].shape[0]
condition2 = datadf.loc[(datadf["r_min_x"] < 8) & (datadf["r_max_x"] > 16) ].shape[0]
I have a dataframe like this
data = { 'number':['one','two','three'],
'l_min_x':[6,6,6],
'l_max_x':[12,18,15],
'r_min_x':[6,6,6],
'r_max_x':[17,19,15]}
datadf= pd.DataFrame(data)
print(datadf)
and I get
number l_min_x l_max_x r_min_x r_max_x
0 one 6 12 6 17
1 two 6 18 6 19
2 three 6 15 6 15
and I want to check and count how many of the rows fulfill a condition.
Now I can do this going row by row like this question but I am wondering if there is another way that takes the whole dataframe and do the following:
-
Check how many rows have
l_min_x
less than a value (for example 7) andl_max_x
greater than another value (say 17)
In this case the answer would be 1 (one) -
Check how many rows have
r_min_x
less than a value (for example 8) andr_max_x
greater than another value (say 16)
In this case the answer would be 2 (two)
Can I do this without iterating over all rows of the dataframe?
I guess you could do this :
condition1= datadf.loc[(datadf["l_min_x"] < 7) & (datadf["l_max_x"] > 17) ].shape[0]
condition2 = datadf.loc[(datadf["r_min_x"] < 8) & (datadf["r_max_x"] > 16) ].shape[0]