Last active
March 26, 2021 00:42
-
-
Save gebelo/902815fd76c080a4a668e96a93cb430d to your computer and use it in GitHub Desktop.
Converting Z-Score to Percentile in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def zptile(z_score): | |
return .5 * (math.erf(z_score / 2 ** .5) + 1) | |
zptile(0.95) | |
# excel says: 0.8289438737 | |
0.8289438736915181 | |
via: http://stackoverflow.com/questions/2782284/function-to-convert-a-z-score-into-a-percentage | |
To get this to work in a dataframe you need to use the APPLY function | |
df["PTILE"]=df["ZSCORE"].apply(zptile) | |
df["PTILE"]=zptile(df["ZSCORE"]) will not work... | |
see: http://stackoverflow.com/questions/23748842/understanding-math-errors-in-pandas-dataframes | |
To iterate over a slice of columns and perform the calculation on each one -- in this case i'm starting with the column 'pop' and taking every column to the right, iterating through, creating a new zscore percentile column called columnname_p: | |
for col in df.ix[:,'pop':]: | |
df[col+"_p"]=((df[col] - df[col].mean())/df[col].std(ddof=0)).apply(zptile)*100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use lambda to iterate over df in single code