Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:38:47+00:00 2026-06-17T06:38:47+00:00

I have a dataframe with numerical columns. For each column I would like calculate

  • 0

I have a dataframe with numerical columns. For each column I would like calculate quantile information and assign each row to one of them. I tried to use the qcut() method to return a list of bins but instead ended up calculating the bins individually. What I thought might exist but I couldn’t find it would be a method like df.to_quintile(num of quantiles). This is what I came up with but I am wondering if there is a more succint/pandas way of doing this.

import pandas as pd

#create a dataframe
df = pd.DataFrame(randn(10, 4), columns=['A', 'B', 'C', 'D'])

def quintile(df, column):
    """
    calculate quintiles and assign each sample/column to a quintile 
    """
    #calculate the quintiles using pandas .quantile() here
    quintiles = [df[column].quantile(value) for value in [0.0,0.2,0.4,0.6,0.8]]
    quintiles.reverse() #reversing makes the next loop simpler

    #function to check membership in quintile to be used with pandas apply
    def check_quintile(x, quintiles=quintiles):
        for num,level in enumerate(quintiles):
            #print number, level, level[1]
            if  x >= level:
                print x, num
                return num+1

    df[column] = df[column].apply(check_quintile)

quintile(df,'A')

thanks,
zach cp

EDIT: After seeing DSMs answer the function can be written much simpler (below). Man, thats sweet.

def quantile(column, quantile=5):
    q = qcut(column, quantile)
    return len(q.levels)- q.labels
df.apply(quantile)
#or
df['A'].apply(quantile)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:38:48+00:00Added an answer on June 17, 2026 at 6:38 am

    I think using the labels stored inside the Categorical object returned by qcut can make this a lot simpler. For example:

    >>> import pandas as pd
    >>> import numpy as np
    >>> np.random.seed(1001)
    >>> df = pd.DataFrame(np.random.randn(10, 2), columns=['A', 'B'])
    >>> df
              A         B
    0 -1.086446 -0.896065
    1 -0.306299 -1.339934
    2 -1.206586 -0.641727
    3  1.307946  1.845460
    4  0.829115 -0.023299
    5 -0.208564 -0.916620
    6 -1.074743 -0.086143
    7  1.175839 -1.635092
    8  1.228194  1.076386
    9  0.394773 -0.387701
    >>> q = pd.qcut(df["A"], 5)
    >>> q
    Categorical: A
    array([[-1.207, -1.0771], (-1.0771, -0.248], [-1.207, -1.0771],
           (1.186, 1.308], (0.569, 1.186], (-0.248, 0.569], (-1.0771, -0.248],
           (0.569, 1.186], (1.186, 1.308], (-0.248, 0.569]], dtype=object)
    Levels (5): Index([[-1.207, -1.0771], (-1.0771, -0.248],
                       (-0.248, 0.569], (0.569, 1.186], (1.186, 1.308]], dtype=object)
    >>> q.labels
    array([0, 1, 0, 4, 3, 2, 1, 3, 4, 2])
    

    or to match your code:

    >>> len(q.levels) - q.labels
    array([5, 4, 5, 1, 2, 3, 4, 2, 1, 3])
    >>> quintile(df, "A")
    >>> np.array(df["A"])
    array([5, 4, 5, 1, 2, 3, 4, 2, 1, 3])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe and would like to calculate the correlation (with Spearman, data
I have a dataframe with some numeric columns. Some row has a 0 value
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a data frame composed of numeric and non-numeric columns. I would like
I have a data.frame with numeric values. How can I replace the each row
I have a dataframe with over 200 columns. The issue is as they were
I have a dataframe with measurements stored as a list by row. Subject Measurements
I have a dataframe(df) with a number of columns, some of which contain categorical
I have a data frame with 22239 rows & 200 columns. The first column
Let's say I have a data frame with numerical values like this AA01.AVG_Beta AA02.AVG_Beta

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.