在熊猫中发布访问索引

时间:2019-06-13 01:12:55

标签: python python-3.x pandas

我正在python3中使用pandas处理庞大的财务数据集。我正在使用多索引,并且想使用loc方法访问子数据框。

我从simfin.com获得了数据。以下是数据的示例:

GGG;103052;100001;Revenues;2018-12-31;-18.132
GGG;103052;100001;COGS;2018-12-31;3.015
GGG;103052;100001;SG&A;2018-12-31;-4.22
GGG;103052;100001;R&D;2018-12-31;-0.123
GGG;103052;100001;EBIT;2018-12-31;-16.804
GGG;103052;100001;EBITDA;2018-12-31;-4.62

这就是我将数据加载到熊猫的方法:

dfRAW = pd.read_csv(database_folder + 'latest.csv', sep=';')
dfRAW.columns = ['ticker', 'simfinid', 'CompanyIndustryClassificationCode', 'indicator', 'date', 'value']

然后,我对该事物进行多索引,因为除“值”之外的任何事物都是索引

dfRAW.set_index(['CompanyIndustryClassificationCode', 'ticker', 'simfinid', 'date', 'indicator'], inplace=True)

到目前为止,一切正常。她来了。如果我想拉一个行业,比如上面的示例数据中所说的103052,我希望看到一个子数据框。这是我的称呼方式:

print(dfRAW.loc['103052'])

我想并希望我应该获得一个包含按价格代码排序的数据框,包括具有103052作为行业代码的所有dfRAW组件。

但是,这会发生:

KeyError: 'the label [103052] is not in the [index]

0 个答案:

没有答案