'How to use dropna() in a loop?

I have a df with 300 columns, and I need to get drop the null values of each column in a loop using its index but I can't seem to make it work.

def funct_one(data):
    for i in range(300):
         #use = data.dropna(subset = data.columns[:, i])
         #use = data.dropna(subset = data.iloc[:, i])
         subset = data.columns[i]
         use = data.dropna(subset = subset)
         if len(use[i]) ...
    

This is the beginning of my function, with a few different things that I have tried. How can I subset dropna() without using the column name?



Solution 1:[1]

dropna will also work on the whole dataframe, and since your data is 300 columns I would recommend this. Is there a reason you want to loop over everything? as opposed to something like no_na = data.dropna()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Elizabeth