Last active
February 17, 2023 10:58
-
-
Save zufanka/e0096e61b4628e48bc15d7a325496318 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # lower bound for the first quantile | |
| previous_q = 0 | |
| # store the medians in a dictionary | |
| row = {} | |
| # column name to calculate the quantiles from | |
| colname = "bedrag" | |
| for q in range(1,11): | |
| # .quantile() wants a decimal between 0 and 1 | |
| q = q/10 | |
| # calculate the quantile | |
| quantile = df[colname].quantile(q) | |
| if q < 1: | |
| # calculate the median of the percentile, lower than the current percentile, but larger than the previous one | |
| quantile_range = df.loc[(df[colname] < quantile) & (df[colname] > previous_q), "bedrag"] | |
| # if calculating the first decile, get only the upper bounds | |
| elif q == 1: | |
| quantile_range = df.loc[(df[colname] > previous_q), colname] | |
| # get median from the range | |
| quantile_median = quantile_range.median() | |
| # add it to the dictionary | |
| row[f"{q}-median"] = quantile_median | |
| # save this quantile the lower bound for the next quantile | |
| previous_q = quantile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment