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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:12:35+00:00 2026-06-18T07:12:35+00:00

I have several data frames that contain all of the same column names. I

  • 0

I have several data frames that contain all of the same column names. I want to append them into a master data frame. I also want to create a column that denotes the original field and then flood it with the original data frames name. I have some code that works.

df_combine = df_breakfast.copy()
df_combine['X_ORIG_DF'] = 'Breakfast'
df_combine = df_combine.append(df_lunch, ignore_index=True)
df_combine['X_ORIG_DF'] = df_combine['X_ORIG_DF'].fillna('Lunch')
# Rinse and repeat

However, it seems inelegant. I was hoping someone could point me to a more elegant solution. Thank you in advance for your time!

Note: Edited to reflect comment!

  • 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-18T07:12:36+00:00Added an answer on June 18, 2026 at 7:12 am

    I would definitely consider restructuring you data in a way the names can be accessed neatly rather than as variable names (if they must be separate to begin with).
    For example a dictionary:

    d = {'breakfast': df_breakfast, 'lunch': df_lunch}
    

    Create a function to give each DataFrame a new column:

    def add_col(df, col_name, col_entry):
        df = df.copy() # so as not to change df_lunch etc.
        df[col_name] = col_entry
        return df
    

    and combine the list of DataFrame each with the appended column ('X_ORIG_DF'):

    In [3]: df_combine = pd.DataFrame().append(list(add_col(v, 'X_ORIG_DF', k)
                                               for k, v in d.items()))
    Out[3]: 
       0  1  X_ORIG_DF
    0  1  2      lunch
    1  3  4      lunch
    0  1  2  breakfast
    1  3  4  breakfast
    

    In this example: df_lunch = df_breakfast = pd.DataFrame([[1, 2], [3, 4]]).

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

Sidebar

Related Questions

I have several data frames, a b c d , each with the same
Running on R 2.13, I want to have a data.frame of several column, the
I have several lists that I'm trying to enter into a data frame: mydates
I have data.frame that contains several factors and i want to rename factor levels
I have several dataframes in which I want to delete each row that matches
I have a data frame with several variables. What I want is create a
I have several data that looks like this: Vector1_elements = T,C,A Vector2_elements = C,G,A
I have several Core Data model version that I have added to my .xcdatamodeld
I have several medium-sized data sets in-memory that I need to be able to
I have several tables that I am selecting data from. I am trying to

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.