1

I have some data that looks largely like the following:

y=numpy.random.uniform(0,1,10) yx_index=[2,5,6,8] yx=numpy.random.normal(0,1,4) sety=pandas.DataFrame(y,columns=['set_y']) subset_yx=pandas.DataFrame(yx,columns=['subset'],index=yx_index) 

output:

set_y= set 0 0.548554 1 0.436084 2 0.192882 3 0.468712 4 0.290172 5 0.462640 6 0.072014 7 0.273997 8 0.242552 9 0.289873 set_x= set 2 0.943326 5 0.462640 6 2.433632 8 0.060528 

set_x is always a subset of set_y. My question is, what is the easiest way to get elements of set_y which have indexes the same as set_x?

So in the above case the desired output would be:

set_z= set 2 0.192882 5 0.462640 6 0.072014 8 0.242552 

1 Answer 1

2

you can use one of many available indexers. I would recommend .ix[] which is usually faster compared to loc / iloc:

In [86]: set_y.ix[set_x.index] Out[86]: set 2 0.192882 5 0.462640 6 0.072014 8 0.242552 
Sign up to request clarification or add additional context in comments.

1 Comment

Nice and elegant. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.