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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:13:03+00:00 2026-05-28T07:13:03+00:00

I have a list of csv files ( file1, file2, … ) that have

  • 0

I have a list of csv files ("file1", "file2", ...") that have two columns but do not have header labels. I’d like to assign them header labels and them as a DataFrame which is indexed by file and then indexed by those column labels. For example, I tried:

import pandas

mydict = {}
labels = ["col1", "col2"]
for myfile in ["file1", "file2"]:
  my_df = pandas.read_table(myfile, names=labels)
  # build dictionary of dataframe records
  mydict[myfile] = my_df

test = pandas.DataFrame(mydict)

this produces a DataFrame, test, indexed by "myfile1", "myfile2"... however, I’d like each of those to be indexed by "col1" and "col2" as well. My questions are:

  1. how can I make it so the first index is file, and second index are columns I assigned (in the variable labels)? So that I can write:

    test["myfile1"]["col1"]

right now, test["myfile1"] only gives me a series of records.

  1. also, how can I then reindex things so that the first indices are the column labels of each file and the second is the filename? So that I can write:

    test["col1"]["myfile1"]

or print test["col1"] and then see the value of "col1" shown for myfile1, myfile2, etc.

  • 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-05-28T07:13:04+00:00Added an answer on May 28, 2026 at 7:13 am

    If you’re using pandas >= 0.7.0 (currently only available in the GitHub repository, though I’ll be making a release imminently!), you can concatenate your dict of DataFrames:

    http://pandas.sourceforge.net/merging.html#more-concatenating-with-group-keys

    In [6]: data
    Out[6]: 
    {'file1.csv':    A       B     
    0  1.0914 -1.3538
    1  0.5775 -0.2392
    2 -0.2157 -0.2253
    3 -2.4924  1.0896
    4  0.6910  0.8992
    5 -1.6196  0.3009
    6 -1.5500  0.1360
    7 -0.2156  0.4530
    8  1.7018  1.1169
    9 -1.7378 -0.3373,
     'file2.csv':    A       B      
    0 -0.4948 -0.15551
    1  0.6987  0.85838
    2 -1.3949  0.25995
    3  1.5314  1.25364
    4  1.8582  0.09912
    5 -1.1717 -0.21276
    6 -0.2603 -1.78605
    7 -3.3247  1.26865
    8  0.7741 -2.25362
    9 -0.6956  1.08774}
    
    
    In [10]: cdf = concat(data, axis=1)
    
    In [11]: cdf
    O    ut[11]: 
       file1.csv          file2.csv         
       A          B       A          B      
    0  1.0914    -1.3538 -0.4948    -0.15551
    1  0.5775    -0.2392  0.6987     0.85838
    2 -0.2157    -0.2253 -1.3949     0.25995
    3 -2.4924     1.0896  1.5314     1.25364
    4      0.6910     0.8992  1.8582     0.09912
    5 -1.6196     0.3009 -1.1717    -0.21276
    6 -1.5500     0.1360 -0.2603    -1.78605
    7 -0.2156     0.4530 -3.3247     1.26865
    8  1.7018     1.1169  0.7741    -2.25362
    9 -1.7378    -0.3373 -0.6956     1.08774
    

    Then if you wish to switch the order of the column indexes, you can do:

    In [14]: cdf.swaplevel(0, 1, axis=1)
    Out[14]: 
       A          B          A          B        
       file1.csv  file1.csv  file2.csv  file2.csv
    0  1.0914    -1.3538    -0.4948    -0.15551  
    1  0.5775    -0.2392     0.6987     0.85838  
    2 -0.2157    -0.2253    -1.3949     0.25995  
    3 -2.4924     1.0896     1.5314     1.25364  
    4  0.6910     0.8992     1.8582     0.09912  
    5 -1.6196     0.3009    -1.1717    -0.21276  
    6 -1.5500     0.1360    -0.2603    -1.78605  
    7 -0.2156     0.4530    -3.3247     1.26865  
    8  1.7018     1.1169     0.7741    -2.25362  
    9 -1.7378    -0.3373    -0.6956     1.08774  
    

    Alternately, and perhaps a bit straightforwardly, you can use a Panel:

    In [16]: p = Panel(data)
    
    In [17]: p
    Out[17]: 
    <class 'pandas.core.panel.Panel'>
    Dimensions: 2 (items) x 10 (major) x 2 (minor)
    Items: file1.csv to file2.csv
    Major axis: 0 to 9
    Minor axis: A to B
    
    In [18]: p = p.swapaxes(0, 2)
    
    In [19]: p
    Out[19]: 
    <class 'pandas.core.panel.Panel'>
    Dimensions: 2 (items) x 10 (major) x 2 (minor)
    Items: A to B
    Major axis: 0 to 9
    Minor axis: file1.csv to file2.csv
    
    In [20]: p['A']
    Out[20]: 
       file1.csv  file2.csv
    0  1.0914    -0.4948   
    1  0.5775     0.6987   
    2 -0.2157    -1.3949   
    3 -2.4924     1.5314   
    4  0.6910     1.8582   
    5 -1.6196    -1.1717   
    6 -1.5500    -0.2603   
    7 -0.2156    -3.3247   
    8  1.7018     0.7741   
    9 -1.7378    -0.6956   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two csv files. In the first file i have a list of
I'd like to delete a selection of columns from a list of CSV files.
Suppose we have files file1.csv , file2.csv , ... , and file100.csv in directory
I have 2 excel files: IDList.csv and Database.csv. IDList contains a list of 300
I have a list of string that I am writing out to a CSV
I have 2 csv files: output.csv output1.csv output.csv has a 5 columns of titles.
I have a Excel CSV files with employee records in them. Something like this:
I have this method that downloads .csv files from yahoo finance and saves them
I have a lot of .csv files in a directory and I'd like to
I have two long list, one from a log file that contains lines formatted

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.