Home > Enterprise >  how to obtain a subset of a dataframe based on column values?
how to obtain a subset of a dataframe based on column values?

Time:04-22

I have a dataframe with the current values in columns:

pop_b
CT (mm) A   B   C   D   adultos_perc    min max class_center    Y
0   100- 110    40  0   0   0   0.000000    100 110 105 inf
1   110-120 72  0   0   0   0.000000    110 120 115 inf
2   120-130 108 12  0   0   0.100000    120 130 125 2.197225
3   130-140 112 41  7   0   0.300000    130 140 135 0.847298
4   140-150 92  70  18  4   0.500000    140 150 145 0.000000
5   150-160 60  98  34  7   0.698492    150 160 155 -0.840129
6   160-170 27  105 36  16  0.853261    160 170 165 -1.760409
7   170-180 0   87  38  21  1.000000    170 180 175 -inf
8   180-190 0   45  28  7   1.000000    180 190 185 -inf
9   190-200 0   15  9   6   1.000000    190 200 195 -inf
10  200-210 0   7   3   2   1.000000    200 210 205 -inf
11  210-220 0   4   2   2   1.000000    210 220 215 -inf
12  220-230 0   6   3   2   1.000000    220 230 225 -inf
13  230-240 0   8   3   2   1.000000    230 240 235 -inf

I wanted to create a new dataframe which has only the rows whose "Y" values aren't 'inf' or '-inf'.

The dataframe has the current dtypes:

CT (mm)          object
A                 int64
B                 int64
C                 int64
D                 int64
adultos_perc    float64
min               int64
max               int64
class_center      int64
Y               float64
dtype: object

CodePudding user response:

You could use between:

out = df[df['Y'].between(-float('inf'), float('inf'), inclusive='neither')]

or gt and lt wrappers chained together with &:

out = df[df['Y'].gt(-float('inf')) & df['Y'].lt(float('inf'))]

Output:

    CT(mm)    A    B   C   D  adultos_perc  min  max  class_center         Y
2  120-130  108   12   0   0      0.100000  120  130           125  2.197225
3  130-140  112   41   7   0      0.300000  130  140           135  0.847298
4  140-150   92   70  18   4      0.500000  140  150           145  0.000000
5  150-160   60   98  34   7      0.698492  150  160           155 -0.840129
6  160-170   27  105  36  16      0.853261  160  170           165 -1.760409
  • Related