Construct Pandas DataFrame from dictionary in form {index: list of row values}

11,818

Solution 1

Alternatively you could use DataFrame.from_items() to construct the DataFrame from your dictionary; this allows you to pass in the column names at the same time.

For example, if d is your dictionary:

d = {0: [50, 45, 0, 0],
     1: [53, 48, 0, 0],
     2: [56, 53, 0, 0],
     3: [54, 49, 0, 0],
     4: [53, 48, 0, 0],
     5: [50, 45, 0, 0]}

The data is d.items() and the orient is again 'index'. The dictionary keys become the index values:

>>> pd.DataFrame.from_items(d.items(), 
                            orient='index', 
                            columns=['A','B','C','D'])
    A   B  C  D
0  50  45  0  0
1  53  48  0  0
2  56  53  0  0
3  54  49  0  0
4  53  48  0  0
5  50  45  0  0

In Python 2 you can use d.iteritems() to yield the contents of the dictionary to avoid creating another list in memory.

Solution 2

One way to do that is the following:

df = pd.DataFrame.from_dict({
0: {"A":50, "B":40},
1: {"A":51, "B":30}}, orient='index')

However, for quick test initialization I would probably prefer your way + then setting the columns.

Solution 3

You could try:

x=pd.DataFrame({0:[50,45],1:[53,48],2:[56,53]}, index=["A","B"]).transpose()

But it's still odd as you are specifying the standard index as keys for your dictionary.

Why not directly

x = pd.DataFrame({"A":[50,53,56],"B":...})
Share:
11,818
birone
Author by

birone

Updated on June 05, 2022

Comments

  • birone
    birone almost 2 years

    I've managed to do this using:

    dft = pd.DataFrame.from_dict({
                        0: [50, 45, 00, 00], 
                        1: [53, 48, 00, 00],
                        2: [56, 53, 00, 00],
                        3: [54, 49, 00, 00],
                        4: [53, 48, 00, 00],
                        5: [50, 45, 00, 00]
                        }, orient='index'
                        )
    

    Done like this, the constructor looks just like the DataFrame making it easy to read/edit:

    >>> dft
        0   1   2   3
    0   50  45  0   0
    1   53  48  0   0
    2   56  53  0   0
    3   54  49  0   0
    4   53  48  0   0
    5   50  45  0   0
    

    But the DataFrame.from_dict constructor doesn't have a columns parameter, so giving the columns sensible names takes an additional step:

    dft.columns = ['A', 'B', 'C', 'D']
    

    This seems clunky for such a handy (e.g. for unit tests) way to initialise DataFrames.

    So I wonder: is there a better way?

  • birone
    birone over 9 years
    "Why not directly x = pd.DataFrame({"A":[50,53,56],"B":...})"? Just to keep the numbers in the initialisation in the same positions as the df, as the question mentions...
  • birone
    birone over 9 years
    The standard index keys are just placeholders, to keep the example simple. Probably I'll need to use datetime objects.