简体   繁体   中英

Python- How to select rows from a multiIndexed data frame

I currently have a data frame in which each row is indexed by a physical 3d coordinate. Here is an example of what the data looks like:

            ind pos
x   y   z       
1.0 8.0 2.0 0   (52.3311, 240.997, 70.7449)
        2.0 1   (54.8969, 241.985, 72.116)
        2.0 2   (51.9996, 240.91, 73.2884)
        2.0 3   (51.765, 240.269, 71.5289)
        2.0 4   (53.5773, 243.276, 69.569)
        2.0 5   (53.1426, 240.779, 71.912)

I want to select all the entries where any of the coordinates (x,y,z) are equal to some value. What is the most efficient way to do this? I have quite a large datset (~ 3 million entries) and I want to select all the rows in which any of x, y, or z are equal to a value 33.

This should do it:

df[1.0,8.0,2.0] 

Also xs help you manage multiindex:

df.xs(('1.0', '8.0', '2.0')) 

PS: Quotes are in case your indexes are strings and not floats

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