1

I am using Dataframe in python and trying to select row index based on multiple column values. Below is the 100th row's value of my dataframe:

>>> df.loc[100]
id                                       100
iid     9cdb7709-38f8-442a-812a-986b5b148161
lat                                 -37.8294
lon                                  144.979
name                      Doryanthes excelsa
Name: 100, dtype: object

I want to select the rows whose id is 100 and lat is -37.8294 by below command:

>>> df[(df['id'] == 100) & (df['lat'] == -37.8294)].index
Int64Index([], dtype='int64')

the above command returns an empty index. I don't understand that I can get the value by df.loc[100] command but why can't I get the row index from the above command?

2
  • 1
    are all your dtypes consistent? is 'lat' isn't a string or anthing like that? Your output would make me think that it is not finding anything that matches those conditions, and dtype inconsistencies is a common way for that to happen. Commented Sep 11, 2017 at 12:13
  • print df['lat'].dtype to check iirc. Commented Sep 11, 2017 at 12:13

1 Answer 1

5

You want select float, but there is precision problem, so get no match and return empty dataframe

So need numpy.isclose:

df1 = df[(df['id'] == 100) & (np.isclose(df['lat'],-37.8294))]

Sample:

df = pd.DataFrame({'id':[100,200],
                   'lat':[-37.82940007,-37.82]})
print (df)
    id      lat
0  100 -37.8294
1  200 -37.8200

df1 = df[(df['id'] == 100) & (df['lat'] == -37.8294)]
print (df1)
Empty DataFrame
Columns: [id, lat]
Index: []

df1 = df[(df['id'] == 100) & (np.isclose(df['lat'],-37.8294))]
print (df1)
    id      lat
0  100 -37.8294
Sign up to request clarification or add additional context in comments.

7 Comments

@Stael - Thank you
this is a good shout - I wouldn't have thought of this, but in this example is it actually the problem? With default display options pandas seems to show floats to 6dp, so it seems unlikely that this is a precision thing. I assumed he's got some strings in there by accident.
yes this solved my issue. It is a precision problem. I will accept the answer in 5 minutes. Thanks.
@ZhaoYi huh, what was the actual value for 'lat' that produced that output then?
@Stael the output of that command prints an array of index not the value.
|

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.