1

I am trying to create a loop to create a loop that helps me drop any columns with a certain amount of missing values. Can someone help me?

for col in df:

"""Delete columns with over 200 missing values"""

if df[col].isnull().sum() > 200:
    pd.DataFrame.drop(col)
FMmach
  • 13
  • 3
  • Possible duplicate of [dropping infinite values from dataframes in pandas?](https://stackoverflow.com/questions/17477979/dropping-infinite-values-from-dataframes-in-pandas) – MT-FreeHK Nov 16 '18 at 04:02

1 Answers1

1
for col in df:
    if df[col].isnull().sum() > 200:
        df=df.drop(col)

Don't drop from pd.DataFrame, but instead drop from df, and also have too assign it back.

Or even better:

df=df.dropna(thresh=len(df)-200,axis=1)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114