To help readability I tend to do something like this:
f1 = (df["col1"] == condition1)
f2 = (df["col2"] == condition2)
df[f1 & f2]
This is equivalent to the 'pandas boolean indexing multiple conditions' method.
This is probably gonna be sacrilege to the Pythonians, but I often wish there was support for some SQL-like syntax when working with (pandas) data frames. It certainly would make the process a lot smoother for some tasks.
This seems to be about doing filtering with Pandas, not pure python. The title should probably be changed to reflect this.
Yeah it looks like his code that this person uploaded isnt escaping the HTML or is being unescaped when it should be escaped.
df.loc[(df['Salary_in_1000']>=100) & (df['Age']< 60) & (df['FT_Team'].str.startswith('S')),['Name','FT_Team']]
Would have been nice to see a comparison of performance, or at least which is suggested style.
One thing that really surprises me: NONE of these methods work with grouped DataFrames.
But grouping data is extremely common in data analysis.
Basically, the strategy with grouped data, is taking the loc approach, and sprinkling in a bunch of additional .transform calls. :/
I strongly prefer .query() for legibility and that it can but used in a pipe. My only problem is that often flake8 will not detect the use of a variable inside of the query string. Has anyone else come across this before?
Some speed comparison on a larger dataset would be interesting
Title should probably clarify that this is with Pandas, that's much more specific and less generally useful than "in Python".
Original title: "Pandas dataframe filter with Multiple conditions"