我有一个 df 与 2 级 MultiIndex 组成['ID', 'Date']。df 按ID,然后按 排序Date。IDs范围从 1-5。我试图删除所有行ID == 1。这有效,但df.index仍显示所有1值。
print(data.head(1))
print(data.index)
data.drop(1, inplace=True)
print(data.head(1))
print(data.index)
输出以下内容:
Inc Exp Inc_Label Exp_Label
ID Date
1 1993-12-31 0.064379 0.004731 0.083734 0.009975
1994-12-31 0.067377 0.009975 0.084116 0.015092
1995-12-31 0.067766 0.015092 0.087881 0.017213
MultiIndex(levels=[[1, 2, 3, 4, 5], ['1968-12-31', '1969-12-31', '1970-12-31', '1971-12-31', '1972-12-31', '1973-12-31', '1974-12-31', '1975-12-31', '1976-12-31', '1977-12-31', '1978-12-31', '1979-12-31', '1980-12-31', '1981-12-31', '1982-12-31', '1983-12-31', '1984-12-31', '1985-12-31', '1986-12-31', '1987-12-31', '1988-12-31', '1989-12-31', '1990-12-31', '1991-12-31', '1992-12-31', '1993-12-31', '1994-12-31', '1995-12-31', '1996-12-31', '1997-12-31', '1998-12-31', '1999-12-31', '2000-12-31', '2001-12-31', '2002-12-31', '2003-12-31', '2004-12-31', '2005-12-31', '2006-12-31', '2007-12-31', '2008-12-31', '2009-12-31', '2010-12-31', '2011-
后来,我尝试创建一个 dict,其中每个键都是IDs 之一,每个值都是原始 df 中对应的子 df:
dict = {index: df.loc[index] for index in df.index.levels[0]}
抛出:
KeyError: 'the label [1] is not in the [index]'
我不明白发生了什么levels,因为 MultiIndex 在下降后保持不变,但labels不同。
动漫人物
相关分类