3

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.

Andrew King
  • 145
  • 10
  • 4
    Possible duplicate of [selecting from multi-index pandas](https://stackoverflow.com/questions/18835077/selecting-from-multi-index-pandas) – rafaelc Jul 17 '18 at 21:25
  • what do you mean ' I want to select all the rows in which and of x, y, or z' are equal to a value 33? – Yuca Jul 17 '18 at 21:30
  • Only know one way - [query()](https://pandas.pydata.org/pandas-docs/stable/indexing.html#the-query-method). Interested to see a comparative analysis of efficiencies of alternatives though. – spinkus Jul 18 '18 at 04:57
  • query() did exactly what I wanted. Thanks! – Andrew King Jul 18 '18 at 15:06

1 Answers1

0

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

Yuca
  • 6,010
  • 3
  • 22
  • 42
  • The thing is, I don't know how to use xs to select slices where any of the values could be what I want. For example, say I make this key value 20. I don't know how to use xs to select rows in which x is 20, or y is 20, or z is 20, or x and y are 20, or y and z are 20, etc. Additionally, x,y,z can take on a wide range of values so I am not able to just search using df[x, y, z] sadly. – Andrew King Jul 17 '18 at 21:32
  • Then your question is not conveying the problem your trying to solve. asking the right question will give you half of the solution. I'm finding hard to follow what you are trying to do. But there are many people smarter than me, they should come to your rescue anytime now – Yuca Jul 17 '18 at 21:58