Home > Software engineering >  How to satisfy two conditions to extract data from a dataframe. one if the columns are > 0 and if
How to satisfy two conditions to extract data from a dataframe. one if the columns are > 0 and if

Time:10-14

the condition is that the values in a column are more than 0 for at least 20 % of the rows

condition = df3[df3 > 0]

for value in df3:
    if condition is 0.2*(df3.iloc[1:, 1:]):
        #subset only columns that satisfy the condition
     
    else:
        pass
    

CodePudding user response:

import numpy as np
import pandas as pd
np.random.seed(0)


df3 = pd.DataFrame(np.random.random((4, 10))-0.8)

larger_zero_count = (df3 > 0).sum(axis=1)

mask = (larger_zero_count / df3.columns.size) <= 0.2

df_selection = df3[mask]

CodePudding user response:

Aggregate with the mean to get the proportion of True and compare to 0.2 (20%):

df3[df3.gt(0).mean(axis=1).gt(0.2)]
  • Related