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

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

提交回复
热议问题