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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:42:57+00:00 2026-06-14T00:42:57+00:00

I have a Pandas dataframe ‘df’ like this : X Y IX1 IX2 A

  • 0

I have a Pandas dataframe ‘df’ like this :

         X   Y  
IX1 IX2
A   A1  20  30
    A2  20  30
    A5  20  30
B   B2  20  30
    B4  20  30

It lost some rows, and I want to fill in the gap in the middle like this:

         X   Y  
IX1 IX2
A   A1  20  30
    A2  20  30
    A3  NaN NaN
    A4  NaN NaN
    A5  20  30
B   B2  20  30
    B3  NaN NaN
    B4  20  30

Is there a pythonic way to do this ?

  • 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-14T00:42:58+00:00Added an answer on June 14, 2026 at 12:42 am

    You need to construct your full index, and then use the reindex method of the dataframe. Like so…

    import pandas
    import StringIO
    datastring = StringIO.StringIO("""\
    C1,C2,C3,C4
    A,A1,20,30
    A,A2,20,30
    A,A5,20,30
    B,B2,20,30
    B,B4,20,30""")
    
    dataframe = pandas.read_csv(datastring, index_col=['C1', 'C2'])
    full_index = [('A', 'A1'), ('A', 'A2'), ('A', 'A3'), 
                  ('A', 'A4'), ('A', 'A5'), ('B', 'B1'), 
                  ('B', 'B2'), ('B', 'B3'), ('B', 'B4')]
    new_df = dataframe.reindex(full_index)
    new_df
          C3  C4
    A A1  20  30
      A2  20  30
      A3 NaN NaN
      A4 NaN NaN
      A5  20  30
    B B1 NaN NaN
      B2  20  30
      B3  20  30
      B4  20  30
    

    And then you can use the fillna method to set the NaNs to whatever you want.

    update (June 2014)

    Just had to revisit this myself…
    In the current version of pandas, there is a function to build MultiIndex from the Cartesian product of iterables. So the above solution could become:

    datastring = StringIO.StringIO("""\
    C1,C2,C3,C4
    A,1,20,30
    A,2,20,30
    A,5,20,30
    B,2,20,30
    B,4,20,30""")
    
    dataframe = pandas.read_csv(datastring, index_col=['C1', 'C2'])
    full_index = pandas.MultiIndex.from_product([('A', 'B'), range(6)], names=['C1', 'C2'])
    new_df = dataframe.reindex(full_index)
    new_df
          C3  C4
    C1 C2
     A  1  20  30
        2  20  30
        3 NaN NaN
        4 NaN NaN
        5  20  30
     B  1 NaN NaN
        2  20  30
        3  20  30
        4  20  30
        5 NaN NaN
    

    Pretty elegant, in my opinion.

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

Sidebar

Related Questions

I have a pandas dataframe object that looks like this: one two three four
I have the data below saved as a pandas dataframe . With this data,
Suppose I have a Pandas dataframe df has columns a,b,c,d...z . And I want
I have a column in a pandas DataFrame that I would like to split
I have a dataframe structured like this: First A B Second bar baz foo
Assume I have a pandas DataFrame with two columns, A and B. I'd like
I have a Pandas dataframe with columns like Order Balance Profit cum (%) I'm
I have a Pandas dataframe, and i want to plot it as matplotlib table.
I have a pandas DataFrame and I want to plot a bar chart that
I have performed some simple z-transforms on some variables in a pandas DataFrame. Of

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.