13

Hi I have a filter 'm' set that is flexible enough to change by me. Sometimes, I want to filter by Car or x_acft_body , or any of the various other fields, etc. Sometime I want to have all of the rows returned by commenting and uncommenting the required lines. But without changing the subsequent code, after the filter 'm' line.

How can I have a filter that will return true for ALL rows, when I don't want the filters applied? For e.g. something like 1==1 but i know this doesn't work.

I don't want to set dfdata.somefield.notnull() etc. as I will not be too sure if this field will be always not null or not. also I DO NOT want to change subsequent code to be like dfdata.groupby. i.e. without the [m]

# set filter if needed
m = (   1==1 #& return true at all times
#         (dfdata.Car == 'PG') #&
#         (dfdata.x_acft_body == 'N')# &
#         (dfdata.Car.isin(['PG', 'VJ', 'VZ']))
)


dft1 = dfdata[m].groupby(['FLD1']).agg({'FLD2': 'count'})
2
  • Is not possible set m = (True) for return all rows?
    – jezrael
    Sep 24, 2017 at 7:39
  • that returns KeyError: True
    – ihightower
    Sep 24, 2017 at 7:40

1 Answer 1

14

You can create bool constant and change final mask by it:

#True for return all rows
m = (dfdata.Car == 'PG') | True

And:

#False for apply filter
m = (dfdata.Car == 'PG') | False

First solutions:

m = [True] * len(df.index)

m = np.repeat(True, len(df.index))
2
  • 1
    #False for apply filter m = (dfdata.Car == 'PG') | False <<<< This is redundant I think... do you mean to say m = (dfdata.Car == 'PG') & False ? only then it will return False all rows.
    – ihightower
    Sep 24, 2017 at 13:21
  • Yes, it is not necessary. If possible True or nothing, False can be omit.
    – jezrael
    Sep 24, 2017 at 13:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.