Skip to content

Instantly share code, notes, and snippets.

@gebelo
Last active March 26, 2021 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gebelo/902815fd76c080a4a668e96a93cb430d to your computer and use it in GitHub Desktop.
Save gebelo/902815fd76c080a4a668e96a93cb430d to your computer and use it in GitHub Desktop.
Converting Z-Score to Percentile in Python
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
@sivarubiny
Copy link

use lambda to iterate over df in single code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment