4
$\begingroup$

Is there a way to more efficiently filter a result on a data frame without having to explicitly save it in a variable and then filter? For instance, in the code below I would like to add something to line 3 to be able to achieve either df1 or df2.

import pandas as pd
df=pd.DataFrame({'A' : [1,2,3,4,5], 'B' : [0,0,0,0,0]})
df=df.var()
df1 = df[df!=0]
df2 = df[df > 3]
$\endgroup$

2 Answers 2

3
$\begingroup$

If you want to filter Pandas Series "on the fly", you can use .loc[] indexer in conjunction with the callable method (for example using lambda function).

Demo:

In [8]: df.var()
Out[8]:
A    2.5
B    0.0
dtype: float64

In [9]: df.var().loc[lambda ser: ser!=0]
Out[9]:
A    2.5
dtype: float64

In [10]: df.var().loc[lambda ser: ser>3]
Out[10]: Series([], dtype: float64)

If you want to filter a DataFrame, then you can use DF.query(...) method:

In [11]: df
Out[11]:
   A  B
0  1  0
1  2  0
2  3  0
3  4  0
4  5  0

In [12]: df.query("A >= 3")
Out[12]:
   A  B
2  3  0
3  4  0
4  5  0
$\endgroup$
1
  • $\begingroup$ awesome, it works, pretty useful $\endgroup$ Commented Jul 25, 2020 at 11:16
0
$\begingroup$

I believe you are looking for this:

import pandas as pd
df=pd.DataFrame({'A' : [1,2,3,4,5], 'B' : [0,0,0,0,0]})
df1 = df[df['B'] != 0]
df2 = df[df['A'] > 3]

Depending on what you are trying to accomplish, you can modify this to meet your needs.

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.