我有一个带有多索引索引的df_ver1。我想删除所有具有不同级别[1]长度然后是2的行。这是我的数据帧。
In [13]: df_ver1
Out[13]:
key nm 0 1 2 3
bar one -0.424972 0.567020 0.276232 -1.087401
two -0.673690 0.113648 -1.478427 0.524988
baz one 0.404705 0.577046 -1.715002 -1.039268
two -0.370647 -1.157892 -1.344312 0.844885
foo one 1.075770 -0.109050 1.643563 -1.469388
qux one -1.294524 0.413738 0.276662 -0.472035
two -0.013960 -0.362543 -0.006154 -0.923061
oof two 1.340309 -1.187678 -2.211372 0.380396
我理想的输出是
In [13]: df_ver1_fixed
Out[13]:
key nm 0 1 2 3
bar one -0.424972 0.567020 0.276232 -1.087401
two -0.673690 0.113648 -1.478427 0.524988
baz one 0.404705 0.577046 -1.715002 -1.039268
two -0.370647 -1.157892 -1.344312 0.844885
qux one -1.294524 0.413738 0.276662 -0.472035
two -0.013960 -0.362543 -0.006154 -0.923061
所以你可以看到我想删除只有1级[1]索引的行。换句话说,我需要在第二级中拥有“一个”和“两个”索引。是否有一种pythonic方法来完成这一步?谢谢!
答案 0 :(得分:3)
这也可行。您实际上可以按多索引key
进行分组,并过滤掉不等于2的组的长度。
df.groupby(by='key').filter(lambda x: len(x) == 2) # keep groups with len 2
正如@Zero建议的那样,我们可以更具体地使用以下内容来指定满足要求的变量集set(['one', 'two'])
。
df.groupby(by='key').filter(
lambda x: set(x.index.get_level_values('nm')) == set(['one', 'two']))
key nm 0 1 2 3
bar one -0.424972 0.567020 0.276232 -1.087401
two -0.673690 0.113648 -1.478427 0.524988
baz one 0.404705 0.577046 -1.715002 -1.039268
two -0.370647 -1.157892 -1.344312 0.844885
qux one -1.294524 0.413738 0.276662 -0.472035
two -0.013960 -0.362543 -0.006154 -0.923061
另一种方法:使用多索引选择
sz = df.groupby("key").size()
indexes = sz[sz == 2].index.tolist() # first-level indexes that we want.
df.loc[indexes] # use loc for selection
key nm 0 1 2 3
bar one -0.424972 0.567020 0.276232 -1.087401
two -0.673690 0.113648 -1.478427 0.524988
baz one 0.404705 0.577046 -1.715002 -1.039268
two -0.370647 -1.157892 -1.344312 0.844885
qux one -1.294524 0.413738 0.276662 -0.472035
two -0.013960 -0.362543 -0.006154 -0.923061
答案 1 :(得分:2)
我认为你需要:
#filter only one and two values by second level
df = df.loc[pd.IndexSlice[:, ['one','two']], :]
#filter by length
df = df[df.groupby(level=0)[df.columns[0]].transform('size') == 2]
print (df)
0 1 2 3
key nm
bar one -0.424972 0.567020 0.276232 -1.087401
two -0.673690 0.113648 -1.478427 0.524988
baz one 0.404705 0.577046 -1.715002 -1.039268
two -0.370647 -1.157892 -1.344312 0.844885
qux one -1.294524 0.413738 0.276662 -0.472035
two -0.013960 -0.362543 -0.006154 -0.923061
另一个解决方案是比较集:
mask = df.reset_index()
.groupby('key')['nm']
.transform(lambda x: set(x) == set(['one','two']))
.values
df = df[mask]
print (df)
0 1 2 3
key nm
bar one -0.424972 0.567020 0.276232 -1.087401
two -0.673690 0.113648 -1.478427 0.524988
baz one 0.404705 0.577046 -1.715002 -1.039268
two -0.370647 -1.157892 -1.344312 0.844885
qux one -1.294524 0.413738 0.276662 -0.472035
two -0.013960 -0.362543 -0.006154 -0.923061