Skip to content
Advertisement

Elimination of outliers with z-score method in Python

I am cleaning a dataset using the z-score with a threshold >3. Below is the code that I am using. As you can, I first calculate the mean and std. After the code goes in a loop and checks for every value the z-score and if it is greater than 3 and, if yes, the value is treated as an outlier which is first added to the list “outlier”. At last the outlier list is deleted for the dataset.

JavaScript

The above code works fine, the fact is that I have to write it for every numerical column. I was trying to create a function that does the same and it is replicable for every numerical column. Below the function:

JavaScript
JavaScript

How can I fix the code?

Advertisement

Answer

You are iterating over column names, which are string, not the actual columns. Try

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement