[英]python pandas multiIndex Dataframe, how to select one level based on iloc
df = pd.DataFrame(np.random.rand(100, 2),
columns=pd.Index(['A', 'B'], name='bar'),
index=pd.date_range('20160101',
periods=100, freq='D', name='foo'))
corr = df.rolling(12).corr()
In old version, this returns a Panel, so the existing code uses 在旧版本中,这将返回一个Panel,因此现有代码使用
corr[0,:,:]
which returns the corr matrix of the first date. 返回第一个日期的corr矩阵。
However, in the new version, corr is a multiIndex DataFrame and the above code fails. 但是,在新版本中,corr是multiIndex DataFrame,并且上面的代码失败。 How can I achieve the same output with minimal change?
如何以最小的变化获得相同的输出?
corr.iloc[0] # only returns first row
corr.iloc[0,:,:] # error
Edit: 编辑:
The desired output is to get the same return as below, but instead of using .loc
with value, use something like .iloc[11]
(ie the correlation matrix corresponding to the 12th value in date) 所需的输出将获得与以下相同的返回值,但不要将
.loc
与值一起使用,而应使用.iloc[11]
类的东西(即,对应于日期中第12个值的相关矩阵)
corr.loc['2016-01-12']
bar A B
foo bar
2016-01-12 A 1.000000 -0.115059
B -0.115059 1.000000
Is that what you want? 那是你要的吗?
In [236]: x = corr.dropna()
In [237]: x.loc[pd.IndexSlice[x.index[0][0], :], :]
Out[237]:
bar A B
foo bar
2016-01-12 A 1.000000 0.158424
B 0.158424 1.000000
The issue as alluded to by @MaxU is that .iloc
is not "MultiIndex-aware"--see here for a discussion. @MaxU暗示的问题是
.iloc
不是“ MultiIndex-aware”-请参阅此处的讨论。
An alternate solution for your case: 您的情况的替代解决方案:
dates = corr.index.get_level_values(0).drop_duplicates()
corr.loc[dates[12]] # correl. matrix for 12th date (0-indexed)
To retain as a DataFrame: 保留为DataFrame:
corr.loc[[dates[12]]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.