birone birone - 7 days ago 5
Python Question

Construct Pandas DataFrame with dict in form {index: list of row values}

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?

Answer

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.

Comments