Y
Y
yamatoko2015-10-05 12:41:55
Python
yamatoko, 2015-10-05 12:41:55

Filtering in pandas - how not to filter if variable is None?

I work with pandas in Python. There is such a code

res = df[
  (df['Column1'] >= column1_min) & (df_item['Column1'] <= column1_max) &
  (df['Column2'].isin(column2) ) &
  (df['Column3'] == column3) &
  #.....

How to make filters (column1_min, column1_max, column2, column3) which are None not applied for filtering?
Now, if any of them is None, then it will filter by values ​​that are also None, that is, it will look for None.
I want that if None, then do not filter by this value at all.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lPolar, 2015-10-05
@lPolar

It seems to me that it can be done a little easier:
1. Select those records where None occurs - select them in a separate frame
2. Apply your code on the remainder
3. Concatenate two frames along the row axis (pd.concat([df1,df2],axis= 0))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question