Skip to content
Advertisement

Pandas – Compute z-score for all columns

I have a dataframe containing a single column of IDs and all other columns are numerical values for which I want to compute z-scores. Here’s a subsection of it:

JavaScript

Some of my columns contain NaN values which I do not want to include into the z-score calculations so I intend to use a solution offered to this question: how to zscore normalize pandas column with nans?

JavaScript

I’m interested in applying this solution to all of my columns except the ID column to produce a new dataframe which I can save as an Excel file using

JavaScript

So basically; how can I compute z-scores for each column (ignoring NaN values) and push everything into a new dataframe?

SIDENOTE: there is a concept in pandas called “indexing” which intimidates me because I do not understand it well. If indexing is a crucial part of solving this problem, please dumb down your explanation of indexing.

Advertisement

Answer

Build a list from the columns and remove the column you don’t want to calculate the Z score for:

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