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

  • Home
  • SEARCH
  • 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 8568115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:02:10+00:00 2026-06-11T18:02:10+00:00

Suppose I have a DataFrame which has a subindex structure like the following, with

  • 0

Suppose I have a DataFrame which has a subindex structure like the following, with ‘date’, ‘tenor’ ‘mat’ and ‘strike’ and where the fields to be observed are stored in the column ‘vol’:

date     tenor   mat strike    vol                                      
20120903 3m      1y  0.25      52.
                     0.50      51.
                     1.00      49.
20120903 3m      5y  0.25      32.
                     0.50      55.
                     1.00      23.
20120903 3m      10y 0.25      65.
                     0.50      55.
                     1.00      19.
20120904 3m      1y  0.25      32.
                     0.50      57.
                     1.00      44.
20120904 3m      5y  0.25      54.
                     0.50      50.
                     1.00      69.
20120904 3m      10y 0.25      42.
                     0.50      81.
                     1.00      99.

Say I want to reorganize this data by getting a new dataframe with subindexes ‘date’ + ‘tenor’ and with ‘values’ given by a 3d array composed by ‘mat’, ‘strike’ and ‘vol’ from the original dataframe in a manner like this:

date     tenor   values                                                       
20120903 3m      [[1y,5y,10y],[0.25, 0.50, 1.00], [52., 51., 49.],
                                                  [32., 55., 23.],
                                                  [65., 55., 19.]]
20120904 3m      [[1y,5y,10y],[0.25, 0.50, 1.00], [32., 57., 44.],
                                                  [54., 50., 69.],
                                                  [42., 81., 99.]]

I tried with various attempts of ‘unstack’, ‘groupby’ and ‘pivot’ but with no success. I could only reach my objective byusing a lot of python vector manipulation, but this was a slow and inefficient procedure. Is there any specific, more efficient pandas procedure in order to get the same result? I’m getting lost at this…
Thanks for your help,
Maurizio

  • 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-11T18:02:11+00:00Added an answer on June 11, 2026 at 6:02 pm

    How about something like this:

    In [111]: df
    Out[111]: 
                    mat  strike  vol
    date     tenor                  
    20120903 3m      1y    0.25   52
             3m      1y    0.50   51
             3m      1y    1.00   49
             3m      5y    0.25   32
             3m      5y    0.50   55
             3m      5y    1.00   23
             3m     10y    0.25   65
             3m     10y    0.50   55
             3m     10y    1.00   19
    20120904 3m      1y    0.25   32
             3m      1y    0.50   57
             3m      1y    1.00   44
             3m      5y    0.25   54
             3m      5y    0.50   50
             3m      5y    1.00   69
             3m     10y    0.25   42
             3m     10y    0.50   81
             3m     10y    1.00   99
    
    In [112]: def agg_func(x):
        mats = list(x.mat.unique())
        strikes = list(x.strike.unique())
        vols = x.pivot('mat', 'strike', 'vol').reindex(mats, columns=strikes)
        return [mats, strikes, vols.values.tolist()]
       .....: 
    
    In [113]: rs = df.groupby(level=['date', 'tenor']).apply(agg_func)
    
    In [114]: rs
    Out[114]: 
    date      tenor
    20120903  3m       [['1y', '5y', '10y'], [0.25, 0.5, 1.0], [[52.0...
    20120904  3m       [['1y', '5y', '10y'], [0.25, 0.5, 1.0], [[32.0...
    
    In [115]: rs.values[0]
    Out[115]: 
    [['1y', '5y', '10y'],
     [0.25, 0.5, 1.0],
     [[52.0, 51.0, 49.0], [32.0, 55.0, 23.0], [65.0, 55.0, 19.0]]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
Suppose I have the following dataframe: df <- data.frame(BR.a=rnorm(10), BR.b=rnorm(10), BR.c=rnorm(10), USA.a=rnorm(10), USA.b =
Suppose I have a date.frame like: df <- data.frame(a=1:5, b=sample(1:5, 5, replace=TRUE), c=5:1) df
Suppose you have a data frame with the following structure: df <- data.frame(a=c(1,2,3,4), b=c(job1;job2,
Suppose I have an R dataframe that looks like this, where end.group signifies the
Suppose I have the following data frame: Data1 X1 X2 1 15 1 2
Suppose we have the name written in any none-latin letters - languages, like Arabic,
Suppose I have a function that creates data frames. I'd like to run that
Suppose I have a column in a dataframe: A A A A B B
Suppose I have a data frame, df, that looks like: f t1 t2 t3

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.