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 8078523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:49:22+00:00 2026-06-05T15:49:22+00:00

I have a Multindex DataFrame with the following structure: 0 1 2 ref A

  • 0

I have a Multindex DataFrame with the following structure:

       0     1     2     ref
A  B             
21 45  0.01  0.56  0.23  0.02
22 45  0.30  0.88  0.53  0.87
23 46  0.45  0.23  0.90  0.23

What I want to do with it is:
From the columns [0:2] choose the closest value to the column ‘ref’, so the expected result would be:

       closest
A  B             
21 45  0.01
22 45  0.88
23 46  0.23 
  • 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-05T15:49:24+00:00Added an answer on June 5, 2026 at 3:49 pm

    Reconstructing your DataFrame:

    In [1]: index = MultiIndex.from_tuples(zip([21,22,23],[45,45,46]), names=['A', 'B'])
    In [2]: df = DataFrame({0:[0.01, 0.30, 0.45], 
                            1:[0.56, 0.88, 0.23], 
                            2:[0.23, 0.53, 0.90], 
                            'ref': [0.02, 0.87, 0.23]}, index=index)
    In [3]: df
    Out[3]: 
            0     1     2   ref
    A  B                         
    21 45  0.01  0.56  0.23  0.02
    22 45  0.30  0.88  0.53  0.87
    23 46  0.45  0.23  0.90  0.23
    

    I would first get the absolute distance of columns0, 1 and 2 from ref:

     In [4]: dist = df[[0,1,2]].sub(df['ref'], axis=0).apply(np.abs)
     In [5]: dist
     Out[5]: 
             0     1     2
     A  B                   
     21 45  0.01  0.54  0.21
     22 45  0.57  0.01  0.34
     23 46  0.22  0.00  0.67
    

    Given now dist you can determine the column with the min value by row using DataFrame.idxmin:

    In [5]: idx = dist.idxmin(axis=1)
    In [5]: idx
    Out[5]: 
    A   B 
    21  45    0
    22  45    1
    23  46    1
    

    To now generate your new closest, then you simply need to use idx to index df:

    In [6]: df['closest'] = idx.index.map(lambda x: df.ix[x][idx.ix[x]])
    In [7]: df
    Out[7]: 
            0     1     2   ref  closest
    A  B                                  
    21 45  0.01  0.56  0.23  0.02     0.01
    22 45  0.30  0.88  0.53  0.87     0.88
    23 46  0.45  0.23  0.90  0.23     0.23
    

    For the last step, there might be a more elegant way to do it but I’m relatively new to Pandas and that’s the best I can think of right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Pandas Dataframe with a MultiIndex(Z,A): H1 H2 Z A 0
Have someone tried out DeCAL in Delphi 2009? I'm thinking about upgrading from 2007,
have not tested on windows. but in ubuntu when u disconnect from the network,
Have following listener for keyboard ArrowDown event(it's key code is 40 ): window.onload =
Have converted devise new session from erb to Haml but doens't work, this is
Have finally got a responsive site working (of a fashion). What I want to
Have a look at the following code to find X^y. /* Find exponent in
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have data that has this kind of structure. Will be in ascending order by
Have data that has this kind of structure: $input = [ { animal: 'cat',

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.