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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:19:39+00:00 2026-05-25T14:19:39+00:00

I want to create a 2D table format from my data stored in a

  • 0

I want to create a 2D table format from my data stored in a dictionary.

Example:

d = {'ID1':[('Experiment1', 40), (Experiment2, 59), (Experiment3, 65)],    
 'ID2':[('Experiment1', 68), (Experiment2, 21), (Experiment3, 39)],   
 'ID3':[('Experiment1', 57), (Experiment2, 15), (Experiment4, 99)]}

Should give the following table:

    Experiment1     Experiment2     Experiment3     Experiment4

ID1 …40…………………………..59………………………….65…………………………..”
ID2….68………………………….21…………………………..39…………………………..”
ID3….57………………………….15…………………………..”…………………………….99

Where IDx are the row labels and Experimentx are the column names.
If a dataset don’t have a value to the column name, then an empty string should be added as placeholder.

Can anybody help how to do this? Is there any ready format in python, which I could use?

  • 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-25T14:19:40+00:00Added an answer on May 25, 2026 at 2:19 pm

    A quick and dirty implementation (fixing your typos in the question …)

    d = {'ID1':[('Experiment1', 40), ('Experiment2', 59), ('Experiment3', 65)],    
     'ID2':[('Experiment1', 68), ('Experiment2', 21), ('Experiment3', 39)],   
     'ID3':[('Experiment1', 57), ('Experiment2', 15), ('Experiment4', 99)]}
    
    COLUMN_WIDTH = 20
    STRING_WHEN_MISSING = '""'
    PADDING_STRING = "."
    
    
    #first determine what are the columns - from the first line
    columns = ["id"] #there is always at least the id column
    columns = columns + [colName for (colName, colValue) in d.items()[0][1]]
    
    print "".join([ col.ljust(COLUMN_WIDTH) for col in columns])
    
    #browse the lines, order by ID Ascending
    for (rowId, rowValues) in sorted(d.items(), key= lambda x: x[0].lower()):
        #print rowId
        #print rowValues
        rowValuesDict = dict(rowValues) #make it a dict with access by key
        rowValuesDict["id"] = rowId
        #print rowValuesDict
        print "".join([ str(rowValuesDict.get(col, STRING_WHEN_MISSING)).ljust(COLUMN_WIDTH, PADDING_STRING) for col in columns])
    

    This prints out :

    id                  Experiment1         Experiment2         Experiment3         
    ID1.................40..................59..................65..................
    ID2.................68..................21..................39..................
    ID3.................57..................15..................""..................
    

    Note :

    The format of your orginal dictionary is a bit strange … I would expect more something like this :

    d = [('ID1',{'Experiment1': 40, 'Experiment2':59, 'Experiment3':65}),    
         ('ID2',{'Experiment1': 68, 'Experiment2':21, 'Experiment3':39})] 
    

    Some Comments:

    For this kind of things, you want to read about some Python string methods : center(), ljust() and rjust() that add characters before and/or after a string to force its total width.

    Appart from that, the idea is mostly about looping through lists/dictionaries and extract value.

    Note the use of dict method get() that allows to have a default value when no value exists for a given key.

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

Sidebar

Related Questions

Data roughly in the format A B C ID1 ID2 0.5 ID1 ID3 0.7
How to select a same database? I want to create a table from already
I want to create a table of friends with personal information and log on
I want to create a table like myTable = { [0] = { [a]
I want to create a table in MS SQL Server 2005 to record details
I want to create a table structure with checkbox for each row and each
i want to create a table : products which looks like (these are columns
I would want to create a table with the same widths (table and its
I want to create a html table with a 1pt black outer border and
I want to do something like this: create table app_users ( app_user_id smallint(6) not

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.