简体   繁体   中英

selecting rows based on multiindex comparison

df = pd.DataFrame([10,20,30,40], index=[[1,1,2,2],[1,4,3,2]])
df.index.names=['a','b']

I would like to keep the rows in which index a equals index b :

      0
1 1  10
2 2  40

I thought maybe a lambda could work in this case

df.index[lambda x: x[0]==x[1]]

but it doesn't. How could I get the result above?

May be something like below, where you can check if length of each multiindex set is 1:

df[df.index.map(lambda x: len(set(x)))==1]

      0
a b    
1 1  10
2 2  40

get_level_values will work for this

df[df.index.get_level_values(0)==df.index.get_level_values(1)]
Out[455]: 
      0
a b    
1 1  10
2 2  40

To fix your code

df[df.index.map(lambda x: x[0]==x[1])]
Out[457]: 
      0
a b    
1 1  10
2 2  40

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM