Python3.4 : Dataframe From Dictionary
The following code is meant to populate a pandas DataFrame. Unfortunately the output is coming out weird. output = {'Revenue' : [1456216, 549514, 489461], 'Cost of Revenue' : [156
Solution 1:
Instead of passing output.values()
, you could pass list(output.values())
:
>>> pd.DataFrame(list(output.values()), index=output.keys(), columns=years)
year1 year2 year3
Cost of Revenue 1565486498464156131
Gross Profit 456465565165651613
Revenue 1456216549514489461
You could also write
>>>df = pd.DataFrame.from_dict(output, orient='index')>>>df.columns = years>>>df
year1 year2 year3
Cost of Revenue 1565486 498464 156131
Gross Profit 456465 565165 651613
Revenue 1456216 549514 489461
The DataFrame constructor is asked to do a lot of things, and sometimes I think it's simpler to break it up.
As for why this is happening, there's a lot of special-casing on type in the constructor, and this branch:
elifisinstance(data, (list, types.GeneratorType)):
isn't picking up the dict_values
object because it's neither a list nor a generator.
Post a Comment for "Python3.4 : Dataframe From Dictionary"