重新索引MultiIndex数据框的特定级别 [英] Reindexing a specific level of a MultiIndex dataframe

查看:73
本文介绍了重新索引MultiIndex数据框的特定级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个索引的DataFrame,并希望通过其中一个索引对其进行重新索引.

I have a DataFrame with two indices and would like to reindex it by one of the indices.

from pandas_datareader import data
import matplotlib.pyplot as plt
import pandas as pd

# Instruments to download
tickers = ['AAPL']

# Online source one should use
data_source = 'yahoo'

# Data range
start_date = '2000-01-01'
end_date = '2018-01-09'

# Load the desired data
panel_data = data.DataReader(tickers, data_source, start_date, end_date).to_frame()
panel_data.head()

重新编制索引如下:

# Get just the adjusted closing prices
adj_close = panel_data['Adj Close']

# Gett all weekdays between start and end dates
all_weekdays = pd.date_range(start=start_date, end=end_date, freq='B')

# Align the existing prices in adj_close with our new set of dates
adj_close = adj_close.reindex(all_weekdays, method="ffill")

最后一行给出以下错误:

The last line gives the following error:

TypeError: '<' not supported between instances of 'tuple' and 'int'

这是因为DataFrame索引是一个元组列表:

This is because the DataFrame index is a list of tuples:

panel_data.index[0]

(Timestamp('2018-01-09 00:00:00'), 'AAPL')

是否可以重新索引adj_close?顺便说一句,如果我不使用to_frame()将Panel对象转换为DataFrame,则重新索引将按原样工作.但是似乎不建议使用Panel对象...

Is it possible to reindex adj_close? By the way, if I don't convert the Panel object to a DataFrame using to_frame(), the reindexing works as it is. But it seems that Panel objects are deprecated...

推荐答案

如果您希望在某个级别上重新编制索引,则reindex接受一个level参数,您可以传递-

If you're looking to reindex on a certain level, then reindex accepts a level argument you can pass -

adj_close.reindex(all_weekdays, level=0)

传递level参数时,不能同时传递method参数(reindex引发TypeError),因此可以在-

When passing a level argument, you cannot pass a method argument at the same time (reindex throws a TypeError), so you can chain a ffill call after -

adj_close.reindex(all_weekdays, level=0).ffill()

这篇关于重新索引MultiIndex数据框的特定级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆