I am trying to drop columns according to the amount of NaN values they contain. The default threshold is 0.5, but should be able to be changed.
The code I have come up with so far is as follows:
def drop_cols_na(df, threshold=0.5):
for column in df.columns:
if df[column].isna().sum() / df.shape[0] >= threshold:
df.drop([column], axis=1, inplace=True)
return df
The code runs fine, but is having no effect and does not remove any columns. Any advice would be much appreciated! Thanks