The following is a way to tidy up your syntax, but essentially do the same as the other answers:
>>> mydict = {'one': [1,2,3], 2: [4,5,6,7], 3: 8} >>> dict_df = pd.DataFrame({ key:pd.Series(value) for key, value in mydict.items() }) >>> dict_df one 2 3 0 1.0 4 8.0 1 2.0 5 NaN 2 3.0 6 NaN 3 NaN 7 NaN
A similar syntax exists for lists:
>>> mylist = [ [1,2,3], [4,5], 6 ] >>> list_df = pd.DataFrame([ pd.Series(value) for value in mylist ]) >>> list_df 0 1 2 0 1.0 2.0 3.0 1 4.0 5.0 NaN 2 6.0 NaN NaN
Another syntax for lists:
>>> mylist = [ [1,2,3], [4,5], 6 ] >>> list_df = pd.DataFrame({ i:pd.Series(value) for i, value in enumerate(mylist) }) >>> list_df 0 1 2 0 1 4.0 6.0 1 2 5.0 NaN 2 3 NaN NaN
In all of these cases, you should be careful to check which pandas
data type will be guessed for your columns. Columns containing any (missing) NaN
values will be converted, for example, to a floating point number.
source share