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

前端 未结 3 1678
北恋
北恋 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: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
    

提交回复
热议问题