Home > front end >  Python pandas filter by column name and drop column if inferior as reference column value
Python pandas filter by column name and drop column if inferior as reference column value

Time:10-17

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.

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

CodePudding user response:

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
  • Related