Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python pandas filter by column name and drop column if inferior as reference column value

I want to drop all column that contain PP in the name if their values are inferior or equal to the column ‘price’.

I think need to exclude the highPrice column and lowPrice column because they does not contain PP.
and use something like this:

df = df[df.filter <= df.price]

but i dont know how to do the filter thing and how to include it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is my df:

price   highPrice     lowPrice     PP_1     PP_2     PP_3     PP_4    PP_5     PP_6     PP_7
  1.1         1.2          1.0      0.1      0.2     0.3       0.8     1.1      1.5      2.2

my expected result is:

price   highPrice     lowPrice     PP_5     PP_6     PP_7
  1.1         1.2          1.0      1.1      1.5      2.2

>Solution :

Filter columns with PP for greter or equal like lowPrice by DataFrame.filter, get all missing columns with DataFrame.reindex and filter columns by DataFrame.loc:

df = df.loc[:, df.filter(like='PP').ge(df['lowPrice'], axis=0).reindex(df.columns, fill_value=True, axis=1)]
print (df)
   price  highPrice  lowPrice  PP_5  PP_6  PP_7
0    1.1        1.2       1.0   1.1   1.5   2.2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading