Home > database >  How to look for specific values and return an index in a dataframe?
How to look for specific values and return an index in a dataframe?

Time:11-10

Hello I have a dataframe that looks like this:

Index      X            Y           Z           a           b           c        u    v
0      -1.462916    -1.299055   3.015321    -0.727074   2.697548    0.822541    259 -106
1      -1.397448    -1.267354   2.935434    -0.636784   2.755215    0.836514    273 -108
2      -1.384987    -1.269575   2.937742    -0.638735   2.767929    0.836798    279 -108
3      -1.367630    -1.281251   2.959687    -0.661788   2.787479    0.834445    293 -109
4      -1.349895    -1.278706   2.951603    -0.651958   2.804420    0.836097    300 -110
... ... ... ... ... ... ... ... ...
33817   0.744647    0.701664    1.618279    1.717619    4.655669    -0.186012   1655    1169
33818   0.723408    0.674251    1.556330    1.753335    4.632607    -0.129156   1662    1168
33819   0.752354    0.693699    1.602645    1.727306    4.662931    -0.171157   1669    1168
33820   0.681987    0.622136    1.438694    1.821023    4.587733    -0.021106   1675    1167
33821   0.765870    0.691312    1.600311    1.729280    4.676410    -0.168285   1682    1166

So I have a pair of u and v values (the last two columns). So what I want to ask is, if for example I have a value of u = 279 and a value of v = -108, my output would be Index = 2, is there a way to do this?

I tried doing the code below but it only looks for one column only.

v_location = a.loc[a['v'] == -108].index[1]
v_location

[output] = 2

As you can see, this method only looks for values within one column (e.g. v column) and may give wrong index values, as you can see, index=1 and index=2 are both v == -108.

To simply put, I want my input to be values from column u and column v as a pair and would give an index value as an output.

I hope you can hekp me with this one. Thank you very much :)

CodePudding user response:

You can use multiple conditions in the expression like this df['u'].eq(279) & df['v'].eq(-108)

print(a[a['u'].eq(279) & a['v'].eq(-108)].index[0])

Prints:

2

CodePudding user response:

Try isin() function from dataframe. df.isin([-100])

  • Related