简体   繁体   中英

Pandas loc: Multiindexing and selecting rows with specific index values

I have a dataframe having 3 columns ABC

A B C
1 2 10
2 3 20
3 0 30

I have indexed column A,B and sorted

df = ( df.set_index([A,B]).sort_index())

I want to select rows with index A(1,2) and index B (2,3)

df1 = df.loc[[[1,2],[2,3]]]

It throws an error.. what am i doing wrong.. I did experiment with few other things and not able to come with a solution..

IIUC, you use tuples, since that's how MultiIndex values are hashed (since lists cannot be hashed).

print(df.loc[[(1, 2), (2, 3)]])
      C
A B
1 2  10
2 3  20

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