Similar to this post, I am removing rows with >50% missing data to get a more reliable and complete dataset
# Keep only the rows with at least x% non-NA values
# calculate threshold
numOfFeatures=38 # num of features in dataset
x=round(numOfFeatures*0.5) #50%
dfWithDroppedRows = df.dropna(thresh=x)
However, I have a imbalanced dataset, where the majority class makes up almost 93% of my dataset
df['y'].value_counts(normalize=True) * 100
No 92.769441
Yes 7.230559
Therefore, I want to edit the above code to remove rows with >50% missing data from sample from the majority class only, so I do not lose samples from the minority class.
To do this, I have tried:
dfWithDroppedRows = df[df['y'] == 'No'].dropna(thresh=x)
But this results in dfWithDroppedRows containing only reduced rows from majority class and not the samples from the minority class. I suppose I can fix this by concatenating dfWithDroppedRows with df[df['y'] == 'Yes'] but I feel there should be a more straightforward way of doing this. Any suggestions?