[英]Slicing a multiindex on Pandas
I've got a dataframe with a multiindex of the form:我有一个 dataframe 具有以下形式的多索引:
(label, date)
where label
is a string and date
is a DateTimeIndex.其中label
是字符串, date
是 DateTimeIndex。
I want to slice my dataframe by date
;我想按date
切片我的 dataframe ; say for example, I want to get all the rows between 2007 and 2009:例如,我想获取 2007 年到 2009 年之间的所有行:
df.loc[:, '2007':'2009']
It seems like the second part (where I've put the date) is actually slicing the column.似乎第二部分(我放日期的地方)实际上是在对列进行切片。
How do I slice on date
?我如何切date
?
You can check partial string indexing :您可以检查部分字符串索引:
DatetimeIndex partial string indexing also works on a DataFrame with a MultiIndex: DatetimeIndex 部分字符串索引也适用于具有 MultiIndex 的 DataFrame:
df = pd.DataFrame(np.random.randn(20, 1),
columns=['A'],
index=pd.MultiIndex.from_product(
[['a', 'b'], pd.date_range('20050101', periods=10, freq='10M'),
]))
idx = pd.IndexSlice
df1 = df.loc[idx[:, '2007':'2009'], :]
print (df1)
A
a 2007-07-31 0.325027
2008-05-31 -1.307117
2009-03-31 -0.556454
b 2007-07-31 1.808920
2008-05-31 1.245404
2009-03-31 -0.425046
Another idea is use loc
with axis=0
parameter:另一个想法是使用带有axis=0
参数的loc
:
df1 = df.loc(axis=0)[:, '2007':'2009']
print (df1)
A
a 2007-07-31 0.325027
2008-05-31 -1.307117
2009-03-31 -0.556454
b 2007-07-31 1.808920
2008-05-31 1.245404
2009-03-31 -0.425046
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.