微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python 3.4:Pandas DataFrame不响应有序字典

我正在使用有序词典填充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是来自多个列表的压缩字典,即:标签和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

解决方法:

这看起来像是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 = indexs [0]提取键,但是随后检查它是否为列表,如果是,则对结果进行排序:result = Index(sorted(result)),这就是为什么要获得此结果的原因.

Issue here

duplicate issue

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐