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

user529499

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?

noah

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]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to filter Pandas rows by another Dataframe columns?

From Dev

pandas DataFrame filter by rows and columns

From Dev

How do you filter out rows with NaN in a panda's dataframe

From Dev

How do you filter out rows with NaN in a panda's dataframe

From Dev

Constructing pandas dataframe with rows conditional on their not existing in another dataframe python

From Java

how do you filter pandas dataframes by multiple columns

From Dev

how do you filter a Pandas dataframe by a multi-column set?

From Dev

How to add rows into existing dataframe in pandas? - python

From Dev

Conditional aggregation on pandas dataframe columns with combining 'n' rows into 1 row

From Dev

Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe

From Dev

How do you use pandas.DataFrame columns as index, columns, and values?

From Dev

How to filter pandas dataframe columns by partial label

From Dev

how to switch columns rows in a pandas dataframe

From Dev

How to transpose dataframe columns into rows in pandas

From Dev

How do you pop multiple columns off a Pandas dataframe, into a new dataframe?

From Dev

How do you transpose a dask dataframe (convert columns to rows) to approach tidy data principles

From Dev

How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

From Dev

Conditional operations for rows in pandas dataframe

From Dev

Filter dataframe rows depending on columns

From Dev

How do you stack two Pandas Dataframe columns on top of each other?

From Dev

How do you combine Range and Individual / Array Columns Selection in Pandas DataFrame

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

How do I drop rows from a Pandas dataframe based on data in multiple columns?

From Dev

Pandas DataFrame, How do I remove all columns and rows that sum to 0

From Dev

How to operate conditional calculation between columns in pandas dataframe?

From Dev

How do you shift Pandas DataFrame with a multiindex?

From Dev

How do you represent na in a Pandas DataFrame?

From Dev

How do you represent na in a Pandas DataFrame?

From Dev

How to filter rows with zero in some columns from the dataframe?

Related Related

  1. 1

    How to filter Pandas rows by another Dataframe columns?

  2. 2

    pandas DataFrame filter by rows and columns

  3. 3

    How do you filter out rows with NaN in a panda's dataframe

  4. 4

    How do you filter out rows with NaN in a panda's dataframe

  5. 5

    Constructing pandas dataframe with rows conditional on their not existing in another dataframe python

  6. 6

    how do you filter pandas dataframes by multiple columns

  7. 7

    how do you filter a Pandas dataframe by a multi-column set?

  8. 8

    How to add rows into existing dataframe in pandas? - python

  9. 9

    Conditional aggregation on pandas dataframe columns with combining 'n' rows into 1 row

  10. 10

    Python/Pandas: filter and organize the rows and columns of a dataframe based on another dataframe

  11. 11

    How do you use pandas.DataFrame columns as index, columns, and values?

  12. 12

    How to filter pandas dataframe columns by partial label

  13. 13

    how to switch columns rows in a pandas dataframe

  14. 14

    How to transpose dataframe columns into rows in pandas

  15. 15

    How do you pop multiple columns off a Pandas dataframe, into a new dataframe?

  16. 16

    How do you transpose a dask dataframe (convert columns to rows) to approach tidy data principles

  17. 17

    How do I calculate mean on filtered rows of a pandas dataframe and append means to all columns of original dataframe?

  18. 18

    Conditional operations for rows in pandas dataframe

  19. 19

    Filter dataframe rows depending on columns

  20. 20

    How do you stack two Pandas Dataframe columns on top of each other?

  21. 21

    How do you combine Range and Individual / Array Columns Selection in Pandas DataFrame

  22. 22

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  23. 23

    How do I drop rows from a Pandas dataframe based on data in multiple columns?

  24. 24

    Pandas DataFrame, How do I remove all columns and rows that sum to 0

  25. 25

    How to operate conditional calculation between columns in pandas dataframe?

  26. 26

    How do you shift Pandas DataFrame with a multiindex?

  27. 27

    How do you represent na in a Pandas DataFrame?

  28. 28

    How do you represent na in a Pandas DataFrame?

  29. 29

    How to filter rows with zero in some columns from the dataframe?

HotTag

Archive