How to use .le() and .ge() when filtering pandas data frame columns?

前端 未结 3 1639
北恋
北恋 2021-01-20 07:06

Here is an example pandas DataFrame:

import pandas as pd
import numpy as np

data = {\"first_column\": [\"item1\", \"item2\", \"item3\", \"item4\", \"item5\"         


        
相关标签:
3条回答
  • 2021-01-20 07:42

    You can use between() instead for your Series of interest.

    df['both'] = df.third_column.between(10, 1000).astype(np.uint8)
    

    Yielding

    >>> df
    
      first_column second_column  third_column  both
    0        item1          cat1             5     0
    1        item2          cat1             1     0
    2        item3          cat1             8     0
    3        item4          cat2             3     0
    4        item5          cat2           731     1
    5        item6          cat2           189     1
    6        item7          cat2             9     0
    
    0 讨论(0)
  • 2021-01-20 07:46

    Use & to compound the conditions:

    In [28]:
    df['both'] = df['third_column'].ge(10) & df['third_column'].le(1000)
    df
    
    Out[28]:
      first_column second_column  third_column   both
    0        item1          cat1             5  False
    1        item2          cat1             1  False
    2        item3          cat1             8  False
    3        item4          cat2             3  False
    4        item5          cat2           731   True
    5        item6          cat2           189   True
    6        item7          cat2             9  False
    
    0 讨论(0)
  • 2021-01-20 07:49
    In [11]: df['both'] = df.eval("10 <= third_column <= 1000").astype(np.uint8)
    
    In [12]: df
    Out[12]:
      first_column second_column  third_column  both
    0        item1          cat1             5     0
    1        item2          cat1             1     0
    2        item3          cat1             8     0
    3        item4          cat2             3     0
    4        item5          cat2           731     1
    5        item6          cat2           189     1
    6        item7          cat2             9     0
    

    UPDATE:

    In [13]: df.eval("second_column in ['cat2'] and 10 <= third_column <= 1000").astype(np.uint8)
    Out[13]:
    0    0
    1    0
    2    0
    3    0
    4    1
    5    1
    6    0
    dtype: uint8
    
    0 讨论(0)
提交回复
热议问题