pandas Create a DataFrame from a dictionary of lists


Example

Create a DataFrame from multiple lists by passing a dict whose values lists. The keys of the dictionary are used as column labels. The lists can also be ndarrays. The lists/ndarrays must all be the same length.

import pandas as pd
    
# Create DF from dict of lists/ndarrays
df = pd.DataFrame({'A' : [1, 2, 3, 4],
                       'B' : [4, 3, 2, 1]})
df
# Output:
#       A  B
#    0  1  4
#    1  2  3
#    2  3  2
#    3  4  1

If the arrays are not the same length an error is raised

df = pd.DataFrame({'A' : [1, 2, 3, 4], 'B' : [5, 5, 5]}) # a ValueError is raised

Using ndarrays

import pandas as pd
import numpy as np

np.random.seed(123) 
x = np.random.standard_normal(4)
y = range(4)
df = pd.DataFrame({'X':x, 'Y':y})
df
# Output:           X  Y
#         0 -1.085631  0
#         1  0.997345  1
#         2  0.282978  2
#         3 -1.506295  3

See additional details at: http://pandas.pydata.org/pandas-docs/stable/dsintro.html#from-dict-of-ndarrays-lists