Skip to content
Advertisement

Optimal way to acquire percentiles of DataFrame rows

Problem

I have a pandas DataFrame df:

JavaScript

My desired output, i.e. new_df, contains the 9 different percentiles including the median, and should have the following format:

JavaScript

Attempt

The following was my initial attempt:

JavaScript

However, instead of returning the percentiles of all columns, it calculated these percentiles for each val column and therefore returned 1000 columns. As it calculated the percentiles for each val, all percentiles returned the same values.

I still managed to run the desired task by trying the following:

JavaScript

But this blatantly is such a laborous, manual, and one-dimensional way to achieve the task. What is the most optimal way to find the percentiles of each row for multiple columns?

Advertisement

Answer

You can get use .describe() function like this:

JavaScript

output:

JavaScript

if you want other percentiles than the default 0.25, .05, .075 you can create your own function where you change the values of .describe(percentiles = [0.1, 0.2...., 0.9])

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