Reputation: 10060
Is there a short-cut to drop rows with equal multi indexes? In other words, is there any way to compare indexes without convert them to columns? Here goes how I'm doing so far, but I fill there is something better. Thanks in advance.
>>> df
0 1 2 3
bar one -1.557968 -1.541893 0.804695 0.838684
bar -0.148502 0.778159 1.349751 -3.127047
baz one -0.959053 -1.162494 -0.235791 1.239830
two -1.515304 -0.177502 -1.476930 -1.340431
foo foo -0.797550 0.788936 0.126020 0.229524
two 0.902523 0.717078 -0.038424 0.339487
>>> df = df.reset_index()
>>> cond = (df2['level_0'] == df2['level_1'])
>>> df = df.drop(df[cond].index.values)
>>> df
level_0 level_1 0 1 2 3
0 bar one -1.557968 -1.541893 0.804695 0.838684
2 baz one -0.959053 -1.162494 -0.235791 1.239830
3 baz two -1.515304 -0.177502 -1.476930 -1.340431
5 foo two 0.902523 0.717078 -0.038424 0.339487
Upvotes: 0
Views: 27
Reputation: 24742
You can use df.index.get_level_values()
to get multi-level index values directly.
import pandas as pd
import numpy as np
# replicate your data
a = 'bar bar baz baz foo foo'.split()
b = 'one bar one two foo two'.split()
multi_index = pd.MultiIndex.from_tuples(list(zip(a, b)))
df = pd.DataFrame(np.random.randn(6, 5), index=multi_index)
# do the selection
mask = df.index.get_level_values(0) == df.index.get_level_values(1)
df = df.loc[~mask]
0 1 2 3 4
bar one -0.0646 0.2245 -0.5863 -0.6400 1.4364
baz one 0.6803 1.6834 -1.0671 -1.0762 -0.8407
two -0.4484 1.3863 -3.0398 -0.0031 -0.9646
foo two 0.0264 1.4345 0.5046 1.8788 -1.2081
Upvotes: 1