我使用有序字典填充DataFrame
,但pandas DataFrame
按字母顺序组织列。
labels = income_data[0:-1:4]
year1 = income_data[1:-1:4]
key = eachTicker
value = OrderedDict(zip(labels, year1))
full_dict[key] = value
df = pd.DataFrame(full_dict)
print(df)
如下所示full_dict
是来自多个列表的压缩字典,即:labels
和year1
full_dict
print(full_dict)
OrderedDict([('AAPL', OrderedDict([('Total Revenue', 182795000), ('Cost of Revenue', 112258000), ('Gross Profit', 70537000), ('Research Development', 6041000), ('Selling General and Administrative', 11993000), ('Non Recurring', 0), ('Others', 0), ('Total Operating Expenses', 0), ('Operating Income or Loss', 52503000), ('Total Other Income/Expenses Net', 980000), ('Earnings Before Interest And Taxes', 53483000), ('Interest Expense', 0), ('Income Before Tax', 53483000), ('Income Tax Expense', 13973000), ('Minority Interest', 0), ('Net Income From Continuing Ops', 39510000), ('Discontinued Operations', 0), ('Extraordinary Items', 0), ('Effect Of Accounting Changes', 0), ('Other Items', 0), ('Net Income', 39510000), ('Preferred Stock And Other Adjustments', 0), ('Net Income Applicable To Common Shares', 39510000)]))])
输出的DataFrame
按字母顺序排序,我不知道为什么。我想按full_dict
AAPL AMZN LNKD
Cost of Revenue 112258000 62752000 293797
Discontinued Operations 0 0 0
Earnings Before Interest And Taxes 53483000 99000 31205
Effect Of Accounting Changes 0 0 0
Extraordinary Items 0 0 0
Gross Profit 70537000 26236000 1924970
Income Before Tax 53483000 -111000 31205
Income Tax Expense 13973000 167000 46525
Interest Expense 0 210000 0
Minority Interest 0 0 -427
Net Income 39510000 -241000 -15747
Net Income Applicable To Common Shares 39510000 -241000 -15747
Net Income From Continuing Ops 39510000 -241000 -15747
Non Recurring 0 0 0
Operating Income or Loss 52503000 178000 36135
Other Items 0 0 0
Others 0 0 236946
Preferred Stock And Other Adjustments 0 0 0
Research Development 6041000 0 536184
Selling General and Administrative 11993000 26058000 1115705
Total Operating Expenses 0 0 0
Total Other Income/Expenses Net 980000 -79000 -4930
Total Revenue 182795000 88988000 2218767
答案 0 :(得分:3)
这看起来像DataFrame
ctor中的一个错误,因为它不符合关键顺序,当东方是'列'时,解决方法是使用from_dict
并在指定时转置结果定位为'指数':
In [31]:
df = pd.DataFrame.from_dict(d, orient='index').T
df
Out[31]:
AAPL
Total Revenue 182795000
Cost of Revenue 112258000
Gross Profit 70537000
Research Development 6041000
Selling General and Administrative 11993000
Non Recurring 0
Others 0
Total Operating Expenses 0
Operating Income or Loss 52503000
Total Other Income/Expenses Net 980000
Earnings Before Interest And Taxes 53483000
Interest Expense 0
Income Before Tax 53483000
Income Tax Expense 13973000
Minority Interest 0
Net Income From Continuing Ops 39510000
Discontinued Operations 0
Extraordinary Items 0
Effect Of Accounting Changes 0
Other Items 0
Net Income 39510000
Preferred Stock And Other Adjustments 0
Net Income Applicable To Common Shares 39510000
修改强>
该错误是由index.py中的第5746行引起的:
def _union_indexes(indexes):
if len(indexes) == 0:
raise AssertionError('Must have at least 1 Index to union')
if len(indexes) == 1:
result = indexes[0]
if isinstance(result, list):
result = Index(sorted(result)) # <------ culprit
return result
当它构造索引时,它使用result = indexes[0]
提取密钥,然后检查它是否是列表,如果是,则对结果进行排序:result = Index(sorted(result))
这就是你得到这个结果的原因。