我有一个pdblp
模块为python创建的数据框。 df包含以日期为索引的价格数据,看起来像是多索引/两层列(ticker和'PX_LAST')。为了以一种更简单的方式处理数据,我想摆脱子标题“ PX_LAST”(我不需要它,它与我正在单独执行的某些查找功能混为一谈)。
数据框的外观如下:
df_px_orig.columns
Out[60]:
MultiIndex(levels=[['IKH16 Comdty', 'IKM16 Comdty', 'IKU16 Comdty', 'RXH16 Comdty', 'RXM16 Comdty', 'RXU16 Comdty'], ['PX_LAST']],
labels=[[3, 4, 5, 0, 1, 2], [0, 0, 0, 0, 0, 0]],
names=['ticker', 'field'])
df_px_orig.head()
Out[41]:
ticker RXH16 Comdty RXM16 Comdty RXU16 Comdty IKH16 Comdty IKM16 Comdty \
field PX_LAST PX_LAST PX_LAST PX_LAST PX_LAST
date
2016-01-04 158.79 156.26 155.15 138.28 136.76
2016-01-05 159.05 156.52 155.42 138.73 137.21
2016-01-06 159.69 157.15 156.04 139.01 137.49
2016-01-07 159.18 156.62 155.53 138.18 136.66
2016-01-08 159.66 157.11 155.98 138.53 137.01
ticker IKU16 Comdty
field PX_LAST
date
2016-01-04 136.76
2016-01-05 137.21
2016-01-06 137.49
2016-01-07 136.66
2016-01-08 137.01
我的问题是建议的方法:
此处使用column.map():{Pandas: combining header rows of a multiIndex DataFrame}
此处使用column.droplevel():{Delete second row of header in PANDAS}
这两种方法的结果都是相同的-似乎摆脱了所有数据,剩下的新对象只是一个索引:
dftest = df_px_orig.columns.droplevel(1)
dftest
Out[55]:
Index(['RXH16 Comdty', 'RXM16 Comdty', 'RXU16 Comdty', 'IKH16 Comdty',
'IKM16 Comdty', 'IKU16 Comdty'],
dtype='object', name='ticker')
dftest.head()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-56-f54d042ff4d9> in <module>()
----> 1 dftest.head()
AttributeError: 'Index' object has no attribute 'head'
也许我完全误解了此数据框的结构,应该使用其他方法,帮助将不胜感激。明确地说,我的目标是简单地摆脱“ PX_LAST”级别,并保持其他所有条件不变。
非常感谢
编辑:从用于构建数据框的插件源中添加一些代码:{https://github.com/matthewgilbert/pdblp/blob/master/pdblp/pdblp.py}
def bdh(self, tickers, flds, start_date, end_date, elms=None,
ovrds=None, longdata=False):
"""
Get tickers and fields, return pandas DataFrame with columns as
MultiIndex with levels "ticker" and "field" and indexed by "date".
If long data is requested return DataFrame with columns
["date", "ticker", "field", "value"].
Parameters
----------
tickers: {list, string}
String or list of strings corresponding to tickers
flds: {list, string}
String or list of strings corresponding to FLDS
start_date: string
String in format YYYYmmdd
end_date: string
String in format YYYYmmdd
elms: list of tuples
List of tuples where each tuple corresponds to the other elements
to be set, e.g. [("periodicityAdjustment", "ACTUAL")].
Refer to the HistoricalDataRequest section in the
'Services & schemas reference guide' for more info on these values
ovrds: list of tuples
List of tuples where each tuple corresponds to the override
field and value
longdata: boolean
Whether data should be returned in long data format or pivoted
答案 0 :(得分:0)
请尝试使用:
df.reset_index(level = <level of the index you wish to drop> , drop = True, inplace = True)
它所要做的就是重置索引级别并保持数据不变。