Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter columns of a dataframe between a range using a for loop?

I have a DataFrame like this:

+----------------------------------------------------------------------------------+
|       Total_Production                      Utilization_rate         Avg_Count   |
+----------------------------------------------------------------------------------+
| 0            6.503907                      96.027778                26.194017    |
| 9            6.171308                      95.638889                31.500943    |
| 18           6.144897                      95.986111                27.494776    |
| 27           6.056882                      95.916667                27.525495    |
| 36           6.107343                     105.541667                21.500208    |
| 45           2.139576                      96.166667                27.480307    |
| 54           6.161222                      96.486111                27.498256    |
| 63           1.034555                      56.388889                27.568885    |
| 72           5.021524                      91.069444                30.931702    |
| 81           5.831919                      96.277778                28.284872    |
| 90           2.689860                      62.486111                18.691440    |
| 99           5.227672                      95.555556                31.441761    |
| 108          1.465271                      95.541667                30.064098    |
+----------------------------------------------------------------------------------+

The range is in two series. Highest Range: Total Production 7.744379 Utilization rate 104.534796 Avg Count 29.691733

Lowest Range: Total Production 3.880623 Utilization rate 64.315015 Avg Count 22.652148

What is the best possible way to filter out data of columns? Can i do it using a for loop by iterating rows?

like image 267
Dheeraj Avatar asked Dec 22 '16 10:12

Dheeraj


2 Answers

You can use the & operator to limit the ranges of individual columns:

df[ 
  (3.880623 < df['Total_Production'])  & (df['Total_Production'] < 7.744379) &
  (64.315015 < df['Utilization_rate']) & (df['Utilization_rate'] <  104.534796) &
  (22.652148 < df['Avg_Count'])        & (df['Avg_Count'] < 29.691733)        
]
like image 57
musically_ut Avatar answered Nov 17 '22 07:11

musically_ut


You could use query

In [233]: df.query('3.880623 < Total_Production < 7.744379 and 64.315015 < Utiliza
     ...: tion_rate <  104.534796 and 22.652148 < Avg_Count < 29.691733')
Out[233]:
    Total_Production  Utilization_rate  Avg_Count
0           6.503907         96.027778  26.194017
18          6.144897         95.986111  27.494776
27          6.056882         95.916667  27.525495
54          6.161222         96.486111  27.498256
81          5.831919         96.277778  28.284872
like image 8
Zero Avatar answered Nov 17 '22 06:11

Zero