Reproducible example:
import pandas as pd
tmp_scen = {'name': 'Current conditions',
'actionSet': ['not_irrigated', 'cover_crop_none', 'mar_none'],
'cost': 0.0,
'totalNitrogen': 9.649798364504209,
'totalPhosphorus': 2.4570326616674745,
'sediment': 1.2039077716577735,
'appliedWater': 11.607623110151138,
'infiltrationVolume': 0.7424774065057028,
'acres': 3.382359912104316,
'irrType': 'not_irrigated'}
good_result = pd.DataFrame.from_dict(tmp_scen, orient = 'index')
tmp_scen = {'actionSet': ['not_irrigated', 'cover_crop_none', 'mar_none'],
'name': 'Current conditions',
'cost': 0.0,
'totalNitrogen': 9.649798364504209,
'totalPhosphorus': 2.4570326616674745,
'sediment': 1.2039077716577735,
'appliedWater': 11.607623110151138,
'infiltrationVolume': 0.7424774065057028,
'acres': 3.382359912104316,
'irrType': 'not_irrigated'}
error_result = pd.DataFrame.from_dict(tmp_scen, orient = 'index')
Problem description
Error when creating a dataframe from a dictionary when the first item in the dictionary is a list. See reproducible example above.
TypeError: object of type 'float' has no len()
Expected Output
Out[369]:
0
actionSet [not_irrigated, cover_crop_none, mar_none]
name Current conditions
cost 0
totalNitrogen 9.6498
totalPhosphorus 2.45703
sediment 1.20391
appliedWater 11.6076
infiltrationVolume 0.742477
acres 3.38236
irrType not_irrigated
Output of pd.show_versions()
Comment From: WillAyd
~~This seems to work fine on master, though not with the result you would expect~~. It is ambiguous as to whether you mean to get a 1 column data frame or a 3 column data frame and just made a programming mistake with this kind of construction.
Not sure if there is anything to do here. Note you can construct a Series instead if you want 1D
Comment From: asishm
@WillAyd both examples use the same dictionary albeit with a different order of keys.
Edit: a simpler example:
In [25]: pd.DataFrame.from_dict({'a': [1,2,3], 'b': 1}, orient='index')
# TypeError: object of type 'int' has no len()
In [26]: pd.DataFrame.from_dict({'b': 1, 'a': [1,2,3]}, orient='index')
Out[26]:
0
b 1
a [1, 2, 3]
Note that the initial object itself is the same in both examples. Python dicts aren't guaranteed insertion order until 3.7 (iirc).
Comment From: matttan90
Hey! I'm having a go at this.
Comment From: Dr4cky
Wow, I did not think changing the order would fix my issue, but it did. Having a list as the very first value in a dictionary gave me the 'object of type 'float' has no len()' error.