在将dict转化为dataframe的过程中出现ValueError: arrays must all be same length
错误示例:
d={ 'NUM' : range(3),
'C' : ['c1','c2','c3'],
'D' : ['d1','d2','d3','d4'],
'E' : ['e1','e2','e3'],
'B' : ['b1','b2','b3']}
def newdf():
global d
df = pd.DataFrame(d)
return df
df=newdf()
df
这是因为使用dataframe(dict) 构建dataframe时,每个key会变成一个column,list-like values会变为行,每个values中的list长度不一致就会出现这个错误。
详见:
http://ju.outofmemory.cn/entry/320906
解决方式:
df = pd.DataFrame.from_dict(d, orient='index')
df.transpose()
参考链接:
https://blog.csdn.net/ginsan/article/details/80998911
https://stackoverflow.com/questions/40442014/python-pandas-valueerror-arrays-must-be-all-same-length