一、將一個字典輸入:spa
該字典必須知足:value是一個list類型的元素,且每個key對應的value長度都相同:code
(以該字典的key爲columns)blog
>>> import pandas as pd >>> a = [1,2,3,4,5] >>> b = ["a","b","c"] >>> c = 1 >>> df = pd.DataFrame({"A":a,"B":b,"C":c}) Traceback (most recent call last): ValueError: arrays must all be same length
>>> df = pd.DataFrame([a,b]) # 做爲list輸入,list的元素必須也是list,加入c就錯誤
>>> df
0 1 2 3 4
0 1 2 3 4.0 5.0
1 a b c NaN NaN # 統一一下字典每一個元素值的長度 >>> b = ["a","b","c","d","e"] >>> c = ("232","sdf","345","asd",1) >>> df = pd.DataFrame({"A":a,"B":b,"C":c}) >>> df A B C 0 1 a 232 1 2 b sdf 2 3 c 345 3 4 d asd 4 5 e 1
二、將多個key相同的字典列輸入:pandas
輸入爲一個list,該list各個元素爲dict,且key能夠不一樣(以含最多的key的字典的key爲columns):ast
>>> d1 = {"A":1,"B":2,"C":3} >>> d2 = {"A":"a","B":"b",} >>> d3 = {"A":(1,2),"B":"ab","C":3} >>> li = [d1,d2,d3] >>> df = pd.DataFrame(li) >>> df A B C 0 1 2 3.0 1 a b NaN 2 (1, 2) ab 3.0