Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

python - How do you filter rows in a pandas dataframe conditional on columns existing?

I have a dataframe containing counts of two things, which I've put in columns numA and numB. I want to find the rows where numA < x and numB < y, which can be done like so:

filtered_df = df[(df.numA < x) & (df.numB < y)]

This works when both numA and numB are present. However neither column is guaranteed to appear in the dataframe. If only one column exists, I would still like to filter the rows based on it. This could be easily coded with something along the lines of

if "numA" in df.columns:
    filtered_df = df[df.numA < x]
if "numB" in df.columns:
    filtered_df = filtered_df[filtered_df.numB < y]

But this seems very inefficient, especially since in reality I have 9 columns like this, and each of these requires the same check. Is there a way to achieve the same thing but with code that is more readable, easier to maintain and less tedious to write out?

question from:https://stackoverflow.com/questions/66051076/how-do-you-filter-rows-in-a-pandas-dataframe-conditional-on-columns-existing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want an all-or-nothing type comparison I think a fairly easy way is to use set comparisons:

if(set(list_of_cols_to_check).issubset(df.columns)):
    filtered_df = df[(df.numA < x) & ... & (df.numB < y)]

If you want to perform comparisons for all that do exist it gets a bit more complicated. It is not very different than what you have, but I'd probably do it as follows:

filter = (df.index >= 0) #always true
filter = filter & (df.numA < 4)  if 'numA' in df else filter
filter = filter & (df.numB < 2)  if 'numB' in df else filter
filter = filter & (df.numC < 1)  if 'numC' in df else filter
df[filter]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...