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
L1 L2
a 1 5624
2 1656
3 265677
4 3755
b 5 47
6 85544
e 9 111
10 9478
检查transform
count
out = df[df.groupby(level=0)['col'].transform('count').values>1]
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.