How can I select rows from a MultiIndex DataFrame that have more than 1 level? For example, given the following DataFrame:
col
L1 L2
a 1 5624
2 1656
3 265677
4 3755
b 5 47
6 85544
c 7 97656
d 8 12774
e 9 111
10 9478
I would like to end up with a DataFrame that looks like:
col
one two
a 1 5624
2 1656
3 265677
4 3755
b 5 47
6 85544
e 9 111
10 9478
CodePudding user response:
Check transform
count
out = df[df.groupby(level=0)['col'].transform('count').values>1]
CodePudding user response:
If you only want to consider the first level:
df[df.index.get_level_values('L1').duplicated(keep=False)]
# or
df[df.index.get_level_values(0).duplicated(keep=False)]
If you want to consider all levels:
df[df.index.to_frame().apply(pd.Series.duplicated, keep=False).any(1)]
output:
col
L1 L2
a 1 5624
2 1656
3 265677
4 3755
b 5 47
6 85544
e 9 111
10 9478