Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct Pandas DataFrame from dictionary 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?

like image 277
birone Avatar asked Dec 27 '14 09:12

birone


2 Answers

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.

like image 112
Alex Riley Avatar answered Oct 18 '22 09:10

Alex Riley


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.

like image 22
grasshopper Avatar answered Oct 18 '22 10:10

grasshopper