Python3.4:来自词典

时间:2015-06-15 20:49:04

标签: python python-3.x dictionary dataframe

以下代码用于填充pandas DataFrame。不幸的是,输出结果很奇怪。

output = {'Revenue' : [1456216, 549514, 489461], 'Cost of Revenue' : [1565486, 498464, 156131], 'Gross Profit' : [456465, 565165, 651613] ... ... ... }

years = ['year1', 'year2', 'year3']

df = pd.DataFrame(output.values(), index=output.keys(), columns=years)

print(df)

字典output具有列表值。我想使用output.keys()作为DataFrame索引,列的列表年和output.values()作为数据框中的数据。最终我希望输出如下

                    Year 1          Year 2          Year 3
Revenue             1456216         549514          489461
Cost of Revenue     1565486         498464          156131
Gross Profit        456465          565165          651613
...                 ...             ...             ...
...                 ...             ...             ...

我该怎么做?

当前输出

                                                                                    year1  \
Total Revenue                           ([2218767, 1528545, 972309], [293797, 202908, ...   
Cost of Revenue                         ([2218767, 1528545, 972309], [293797, 202908, ...   
Gross Profit                            ([2218767, 1528545, 972309], [293797, 202908, ...   
Research Development                    ([2218767, 1528545, 972309], [293797, 202908, ...   
Selling General and Administrative      ([2218767, 1528545, 972309], [293797, 202908, ...   
Non Recurring                           ([2218767, 1528545, 972309], [293797, 202908, ...   
Others                                  ([2218767, 1528545, 972309], [293797, 202908, ...   
Total Operating Expenses                ([2218767, 1528545, 972309], [293797, 202908, ...   
Operating Income or Loss                ([2218767, 1528545, 972309], [293797, 202908, ...   
Total Other Income/Expenses Net         ([2218767, 1528545, 972309], [293797, 202908, ...   
Earnings Before Interest And Taxes      ([2218767, 1528545, 972309], [293797, 202908, ...   
Interest Expense                        ([2218767, 1528545, 972309], [293797, 202908, ...   
Income Before Tax                       ([2218767, 1528545, 972309], [293797, 202908, ...   
Income Tax Expense                      ([2218767, 1528545, 972309], [293797, 202908, ...   
Minority Interest                       ([2218767, 1528545, 972309], [293797, 202908, ...   
Net Income From Continuing Ops          ([2218767, 1528545, 972309], [293797, 202908, ...   
Discontinued Operations                 ([2218767, 1528545, 972309], [293797, 202908, ...   
Extraordinary Items                     ([2218767, 1528545, 972309], [293797, 202908, ...   
Effect Of Accounting Changes            ([2218767, 1528545, 972309], [293797, 202908, ...   
Other Items                             ([2218767, 1528545, 972309], [293797, 202908, ...    
Net Income                              ([2218767, 1528545, 972309], [293797, 202908, ...   
Preferred Stock And Other Adjustments   ([2218767, 1528545, 972309], [293797, 202908, ...   
Net Income Applicable To Common Shares  ([2218767, 1528545, 972309], [293797, 202908, ...   

                                                                                year2  \
Total Revenue                           ([2218767, 1528545, 972309], [293797, 202908, ...   
Cost of Revenue                         ([2218767, 1528545, 972309], [293797, 202908, ...   
Gross Profit                            ([2218767, 1528545, 972309], [293797, 202908, ...   
Research Development                    ([2218767, 1528545, 972309], [293797, 202908, ...   
Selling General and Administrative      ([2218767, 1528545, 972309], [293797, 202908, ...   
Non Recurring                           ([2218767, 1528545, 972309], [293797, 202908, ...   
Others                                  ([2218767, 1528545, 972309], [293797, 202908, ...   
Total Operating Expenses                ([2218767, 1528545, 972309], [293797, 202908, ...   
Operating Income or Loss                ([2218767, 1528545, 972309], [293797, 202908, ...   
Total Other Income/Expenses Net         ([2218767, 1528545, 972309], [293797, 202908, ...   
Earnings Before Interest And Taxes      ([2218767, 1528545, 972309], [293797, 202908, ...   
Interest Expense                        ([2218767, 1528545, 972309], [293797, 202908, ...   
Income Before Tax                       ([2218767, 1528545, 972309], [293797, 202908, ...   
Income Tax Expense                      ([2218767, 1528545, 972309], [293797, 202908, ...   
Minority Interest                       ([2218767, 1528545, 972309], [293797, 202908, ...   
Net Income From Continuing Ops          ([2218767, 1528545, 972309], [293797, 202908, ...   
Discontinued Operations                 ([2218767, 1528545, 972309], [293797, 202908, ...   
Extraordinary Items                     ([2218767, 1528545, 972309], [293797, 202908, ...   
Effect Of Accounting Changes            ([2218767, 1528545, 972309], [293797, 202908, ...   
Other Items                             ([2218767, 1528545, 972309], [293797, 202908, ...   
Net Income                              ([2218767, 1528545, 972309], [293797, 202908, ...   
Preferred Stock And Other Adjustments   ([2218767, 1528545, 972309], [293797, 202908, ...   
Net Income Applicable To Common Shares  ([2218767, 1528545, 972309], [293797, 202908, ...   

                                                                                year3  
You get the idea


OrderedDict([('Total Revenue', [2218767, 1528545, 972309]), ('Cost of Revenue', [293797, 202908, 125521]), ('Gross Profit', [1924970, 1325637, 846788]), ('Research Development', [536184, 395643, 257179]), ('Selling General and Administrative', [1115705, 747666, 452898]), ('Non Recurring', ['0', '0', '0']), ('Others', [236946, 134516, 79849]), ('Total Operating Expenses', ['0', '0', '0']), ('Operating Income or Loss', [36135, 47812, 56862]), ('Total Other Income/Expenses Net', [-4930, 1416, 252]), ('Earnings Before Interest And Taxes', [31205, 49228, 57114]), ('Interest Expense', ['0', '0', '0']), ('Income Before Tax', [31205, 49228, 57114]), ('Income Tax Expense', [46525, 22459, 35504]), ('Minority Interest', [-427, '0', '0']), ('Net Income From Continuing Ops', [-15747, 26769, 21610]), ('Discontinued Operations', ['0', '0', '0']), ('Extraordinary Items', ['0', '0', '0']), ('Effect Of Accounting Changes', ['0', '0', '0']), ('Other Items', ['0', '0', '0']), ('Net Income', [-15747, 26769, 21610]), ('Preferred Stock And Other Adjustments', ['0', '0', '0']), ('Net Income Applicable To Common Shares', [-15747, 26769, 21610])])

1 个答案:

答案 0 :(得分:2)

您可以传递output.values()

,而不是传递list(output.values())
>>> pd.DataFrame(list(output.values()), index=output.keys(), columns=years)
                   year1   year2   year3
Cost of Revenue  1565486  498464  156131
Gross Profit      456465  565165  651613
Revenue          1456216  549514  489461

你也可以写

>>> 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

要求DataFrame构造函数执行很多操作,有时我认为分解它更简单。

至于为什么这种情况发生了,构造函数中有很多类型的特殊外壳,而且这个分支:

    elif isinstance(data, (list, types.GeneratorType)):

没有拿起dict_values对象,因为它既不是列表也不是生成器。