2015-06-15 24 views
1

以下代碼旨在填充pandas DataFrame。不幸的是,輸出結果很奇怪。Python3.4:字典中的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])]) 
+0

你的問題到底是什麼? – kennes

+0

@bleh我編輯了問題 –

+0

您的語法是正確的。你的輸出是什麼? – kennes

回答

1

不是傳遞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 

數據幀的構造被要求做了很多的東西,有時我認爲分手更簡單。

至於爲什麼發生這種情況,有在類型很多特殊外殼的構造,以及該分支:

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

不拿起dict_values對象,因爲它既不列表也不是發電機。

+0

它工作完美,但爲什麼我需要將其更改爲列表來做到這一點?你能解釋一下嗎? –

+0

我更喜歡第二種方式,但是如何獲得列標籤呢? –

+0

有無論如何,你可以將這些信息追加到現有的數據幀中? –