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) 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?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150
  • Does this answer your question? [How do I select rows from a DataFrame based on column values?](https://stackoverflow.com/questions/17071871/how-do-i-select-rows-from-a-dataframe-based-on-column-values) And also https://stackoverflow.com/questions/15943769/how-do-i-get-the-row-count-of-a-pandas-dataframe for the count – jmkjaer Oct 06 '22 at 13:05

1 Answers1

1

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]

grymlin
  • 492
  • 1
  • 9