
Consider the following code.
import numpy as np
import pandas as pd
in_dict = dict(a=np.random.rand(3), b=np.random.rand(6), c=np.random.rand(2))
df = pd.DataFrame.from_dict(in_dict)
This fails with the following error
df = pd.DataFrame.from_dict(in_dict)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-2c9e8bf1abe9> in <module>
----> 1 df = pd.DataFrame.from_dict(in_dict)
~\Anaconda3\lib\site-packages\pandas\core\frame.py in from_dict(cls, data, orient, dtype, columns)
1371 raise ValueError("only recognize index or columns for orient")
1372
-> 1373 return cls(data, index=index, columns=columns, dtype=dtype)
1374
1375 def to_numpy(
~\Anaconda3\lib\site-packages\pandas\core\frame.py in __init__(self, data, index, columns, dtype, copy)
527
528 elif isinstance(data, dict):
--> 529 mgr = init_dict(data, index, columns, dtype=dtype)
530 elif isinstance(data, ma.MaskedArray):
531 import numpy.ma.mrecords as mrecords
~\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in init_dict(data, index, columns, dtype)
285 arr if not is_datetime64tz_dtype(arr) else arr.copy() for arr in arrays
286 ]
--> 287 return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
288
289
~\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in arrays_to_mgr(arrays, arr_names, index, columns, dtype, verify_integrity)
78 # figure out the index, if necessary
79 if index is None:
---> 80 index = extract_index(arrays)
81 else:
82 index = ensure_index(index)
~\Anaconda3\lib\site-packages\pandas\core\internals\construction.py in extract_index(data)
399 lengths = list(set(raw_lengths))
400 if len(lengths) > 1:
--> 401 raise ValueError("arrays must all be same length")
402
403 if have_dicts:
ValueError: arrays must all be same length
The solution is simple. I have faced this situation a lot, so posting this here on the blog for easy reference
use orient=’index’
df = pd.DataFrame.from_dict(in_dict, orient='index')
df.head()
0 1 2 3 4 5
a 0.409699 0.098402 0.399315 NaN NaN NaN
b 0.879116 0.460574 0.971645 0.147398 0.939485 0.222164
c 0.747605 0.123114 NaN NaN NaN NaN
df.T
a b c
0 0.409699 0.879116 0.747605
1 0.098402 0.460574 0.123114
2 0.399315 0.971645 NaN
3 NaN 0.147398 NaN
4 NaN 0.939485 NaN
5 NaN 0.222164 NaN
Some related posts you might like