Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

python - Dataframe not filtering correctly when choosing a specific value (Pandas)

my df looks something like this:

URN col1 col2 col3 TEMPPNUM
100279 24.0 75.8 0.1 99.9
100055 52.2 47.8 0.0 100.0
107782 12.3 86.2 1.5 100.0
112956 12.3 86.2 1.5 100.0
139957 19.9 79.9 0.2 100.0

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way of doing it

>>> df.query('TEMPPNUM != 100')

Another way would be with 'mask'

>>> df[df.TEMPPNUM!=100]

Both should produce this output

      URN  col1  col2  col3  TEMPPNUM
0  100279  24.0  75.8   0.1      99.9

EDIT:

If your TEMPPNUM is NOT float/int, then you'd can either convert it to such or simply do that

>>> df.query('TEMPPNUM != 100')

      URN  col1  col2  col3 TEMPPNUM
0  100279  24.0  75.8   0.1     99.9
5  123321  91.0  99.0   0.5      100

>>> mask=df['TEMPPNUM'].astype(int) == 100

>>> df[~mask]

      URN  col1  col2  col3 TEMPPNUM
0  100279  24.0  75.8   0.1     99.9

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...