5

Given a DataFrame with the columns xk and yk, we want to find the indexes of the DataFrame in which the values for xk and yk ==0.

I have it working perfectly fine for just the one column but I cant get it working for both

b = (df[df['xk'] ==0]).index.tolist()

How would I do it for xk and yk at the same time.

1 Answer 1

6

I think you can check if all values are True in compared subset ['xk', 'yk'] by all:

b = df[(df[['xk', 'yk']] == 0).all(1)].index.tolist()

Another solution is add second condition with &:

b = (df[(df['xk']  == 0) & (df['yk'] == 0)].index.tolist())

Sample:

df = pd.DataFrame({'xk':[0,2,3],
                   'yk':[0,5,0],
                   'aa':[0,1,0]})

print (df)
   aa  xk  yk
0   0   0   0
1   1   2   5
2   0   3   0

b = df[(df[['xk', 'yk']] == 0).all(1)].index.tolist()
print (b)
[0]

b1 = (df[(df['xk']  == 0) & (df['yk'] == 0)].index.tolist())
print (b1)
[0]

Second solution is faster:

#length of df = 3k
df = pd.concat([df]*1000).reset_index(drop=True)

In [294]: %timeit df[(df[['xk', 'yk']] == 0).all(1)].index.tolist()
1000 loops, best of 3: 1.21 ms per loop

In [295]: %timeit (df[(df['xk']  == 0) & (df['yk'] == 0)].index.tolist())
1000 loops, best of 3: 828 µs per loop
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that worked perfectly, what about if i wanted to return just the index of the specific row instead of returning the list of all the indices
I am not sure if understand. If use df = pd.DataFrame({'xk':[0,2,0], 'yk':[0,5,0], 'aa':[0,1,0]}) print (df) and then you get DataFrame - a = (df.ix[(df['xk'] == 0) & (df['yk'] == 0)]). Do you need return e.g. first index or second index value by position?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.