There are multiple methods you can use to take a standard python datastructure and create a panda’s DataFrame. For the purposes of these examples, I’m going to create a DataFrame with 3 months of sales information for 3 fictitious companies.

Dictionaries

Before showing the examples below, I am assuming the following imports have been executed:

import pandas as pd from collections import OrderedDict from datetime import date

The “default” manner to create a DataFrame from python is to use a list of dictionaries. In this case each dictionary key is used for the column headings. A default index will be created automatically:

sales = [{ 'account' : 'Jones LLC' , 'Jan' : 150 , 'Feb' : 200 , 'Mar' : 140 }, { 'account' : 'Alpha Co' , 'Jan' : 200 , 'Feb' : 210 , 'Mar' : 215 }, { 'account' : 'Blue Inc' , 'Jan' : 50 , 'Feb' : 90 , 'Mar' : 95 }] df = pd . DataFrame ( sales )

Feb Jan Mar account 0 200 150 140 Jones LLC 1 210 200 215 Alpha Co 2 90 50 95 Blue Inc

As you can see, this approach is very “row oriented”. If you would like to create a DataFrame in a “column oriented” manner, you would use from_dict

sales = { 'account' : [ 'Jones LLC' , 'Alpha Co' , 'Blue Inc' ], 'Jan' : [ 150 , 200 , 50 ], 'Feb' : [ 200 , 210 , 90 ], 'Mar' : [ 140 , 215 , 95 ]} df = pd . DataFrame . from_dict ( sales )

Using this approach, you get the same results as above. The key point to consider is which method is easier to understand in your unique situation. Sometimes it is easier to get your data in a row oriented approach and others in a column oriented. Knowing the options will help make your code simpler and easier to understand for your particular need.

Most of you will notice that the order of the columns looks wrong. The issue is that the standard python dictionary does not preserve the order of its keys. If you want to control column order then there are two options.

First, you can manually re-order the columns:

df = df [[ 'account' , 'Jan' , 'Feb' , 'Mar' ]]

Alternatively you could create your dictionary using python’s OrderedDict .

sales = OrderedDict ([ ( 'account' , [ 'Jones LLC' , 'Alpha Co' , 'Blue Inc' ]), ( 'Jan' , [ 150 , 200 , 50 ]), ( 'Feb' , [ 200 , 210 , 90 ]), ( 'Mar' , [ 140 , 215 , 95 ]) ] ) df = pd . DataFrame . from_dict ( sales )

Both of these approaches will give you the results in the order you would likely expect.

account Jan Feb Mar 0 Jones LLC 150 200 140 1 Alpha Co 200 210 215 2 Blue Inc 50 90 95

For reasons I outline below, I tend to specifically re-order my columns vs. using an OrderedDict but it is always good to understand the options.