0

I have code that looks like this:

    protein   IHD         CM          ARR         VD          CHD         CCD         VOO      
0   q9uku9    0.000000    0.039457    0.032901    0.014793    0.006614    0.006591    0.000000    
1   o75461    0.000000    0.005832    0.027698    0.000000    0.000000    0.006634    0.000000

There's thousands of rows of proteins. However, I want to drop the rows in pandas where all of the values in the row for all of the diseases are less than 0.01. How do I do this?

2 Answers 2

1

You can use loc in combination with any. Basically you want to keep all rows where any value is above or equal to 0.01. Note, I adjusted your example to have the second protein have all values < 0.01.

import pandas as pd

df = pd.DataFrame([
    ['q9uku9', 0.000000, 0.039457, 0.032901, 0.014793, 0.006614, 0.006591, 0.000000 ],
    ['o75461', 0.000000, 0.005832, 0.007698, 0.000000, 0.000000, 0.006634, 0.000000]
], columns=['protein', 'IHD', 'CM', 'ARR', 'VD', 'CHD', 'CCD', 'VOO'])

df = df.set_index('protein')

df_filtered = df.loc[(df >= 0.01).any(axis=1)]

Which gives:

         IHD        CM       ARR        VD       CHD       CCD  VOO
protein                                                            
q9uku9   0.0  0.039457  0.032901  0.014793  0.006614  0.006591  0.0
Sign up to request clarification or add additional context in comments.

Comments

0
>>> df
  protein  IHD        CM       ARR        VD       CHD       CCD  VOO
0  q9uku9  0.0  0.039457  0.032901  0.014793  0.006614  0.006591  0.0
1  o75461  0.0  0.005832  0.027698  0.000000  0.000000  0.006634  0.0
2  d4acr8  0.0  0.001490  0.003920  0.000000  0.000000  0.009393  0.0

>>> df.loc[~(df.select_dtypes(float) < 0.01).all(axis="columns")]
  protein  IHD        CM       ARR        VD       CHD       CCD  VOO
0  q9uku9  0.0  0.039457  0.032901  0.014793  0.006614  0.006591  0.0
1  o75461  0.0  0.005832  0.027698  0.000000  0.000000  0.006634  0.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.