猿问

df.drop(<index_value>) 从 df 中删除具有该索引的所有行

我有一个 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不同。


holdtom
浏览 499回答 1
1回答

动漫人物

您需要从索引中删除未使用的级别,并pandas有一个方法:pandas.MultiIndex.remove_unused_levelsdata.index&nbsp;=&nbsp;data.index.remove_unused_levels()但是如果你只想创建一个唯一组的字典,你真的应该只使用一个groupby:dct&nbsp;=&nbsp;dict((id,&nbsp;gp)&nbsp;for&nbsp;id,&nbsp;gp&nbsp;in&nbsp;data.groupby(level=0))还要避免命名变量,dict因为您会覆盖dict我上面使用的默认函数。样本数据df1 = pd.DataFrame({'id1': [1,1,1,2,2],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'id2': list('ABCAB'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'val': [11,12,13,14,15]})df1 = df1.set_index(['id1', 'id2'])df1.index#MultiIndex(levels=[[1, 2], ['A', 'B', 'C']],#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;labels=[[0, 0, 0, 1, 1], [0, 1, 2, 0, 1]],#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;names=['id1', 'id2'])df2 = df1.drop(1)df2.index#MultiIndex(levels=[[1, 2], ['A', 'B', 'C']],#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;labels=[[1, 1], [0, 1]],#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;names=['id1', 'id2'])df2.index = df2.index.remove_unused_levels()df2.index#MultiIndex(levels=[[2], ['A', 'B']],#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;labels=[[0, 0], [0, 1]],#&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;names=['id1', 'id2'])
随时随地看视频慕课网APP

相关分类

Python
我要回答