How can I find the indices of an array with float values in python?

Avi Gross avigross at verizon.net
Thu Jan 10 17:44:16 EST 2019


Madhavan,

Others have given you reasonable answers out of the ever so many many many
ways you can do what you asked. I offer a question to consider in case your
needs are different or you have not considered other more pythonic ways.

What do you want to do with your data and is this the best way to do it?

The pythonic way often is to minimize the use of features common in
languages like C and use new paradigms like iteration where no index is
needed. As others show, you can do many things like a list comprehension
with an if, perhaps alongside an enumerate to catch the index too. 

So, you have an array of numbers. (numpy style) and you want a way to
designate a subset of those numbers that meet your criterion. Your criterion
is a compound criterion that needs refining. You want numbers between 0 and
15. Are you including one or both endpoints? Any answers you choose to use
will need to be adjusted depending on your answer.

And, you seem to want not the values, but instead the indices of the array
where  those values are stored. Are you sure that is what you need to go to
the next step? Maybe you do under your currently conceived algorithm and the
solutions provided will give you a second array of indices you can use in an
unspecified way later. But if all you want to do is get a potentially
smaller array containing just the values you want, and perhaps iterate on
them or apply a function such as getting the standard deviation, there is
another set of answers that just makes copies of the array in any way you
want, sometimes in multiple steps.

I choose to illustrate below with what some may consider a tutorial and
ignore. If your needs are met, feel free.

Is a numpy array the best data type for your needs. If you were using a
pandas DataFrame where one column was your numpy array and you had other
columns, then one could be an index of N numbers corresponding to your
requested index. If you used commands to create a second DataFrame with only
those rows that matched your criteria, the resulting DataFrame would
automatically have the index numbers, and perhaps much more.

I am NOT saying the added complications are needed, just that without
knowing your goals, it may be other ways are a better fit.

And note that in numpy (and pandas) there are other ways to index. Yes, you
can provide an array of integers to index with like this:

import numpy as np
# Create an array to play with odd numbers.
odds = np.array(range(1,20,2))
odds

==> array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])

# Show indexing of the array using indices.
three_in = [2, 4, 6]
odds[three_in]

==> array([ 5,  9, 13])

But you can also index by a Boolean (True/False) list of the same size like:
# Show Boolean indices
three_bool = [False, False, True, False, True, False, True, False, False,
False]
odds[three_bool]

==> array([ 5,  9, 13])

Same result. 

Now you seem to want numbers in a range. I show a way in this example to get
everything greater than one number but less than or equal to another. Since
my numbers happen to be in order, ...

# Show how to get numbers in a range.
odds[(odds > 3) & (odds <= 15)]

==> array([ 5,  7,  9, 11, 13, 15])

So if you don't need indices, something like the above gives you the data
itself to use. If your data must keep track of multiple sets of data, and
you need an index to deal with them, I would suggest another data structure.
Here for example, I combine the same vector as above along with a list of my
favorite elements 

# Make a DataFrame with an additional field and index.
import pandas as pd
ind = np.array(range(len(odds)))
elementals = np.array(["Hydrogen", "Lithium", "Boron", 
                       "Nitrogen", "Fluorine", "Sodium", 
                       "Aluminum", "Phosphorus", "Chlorine", 
                       "Potassium"])
dictate = { "indexed" : ind, "protons" : odds, "moniker": elementals }

df = pd.DataFrame(dictate)
df

==>
	indexed	protons	moniker
0	0	1	Hydrogen
1	1	3	Lithium
2	2	5	Boron
3	3	7	Nitrogen
4	4	9	Fluorine
5	5	11	Sodium
6	6	13	Aluminum
7	7	15	Phosphorus
8	8	17	Chlorine
9	9	19	Potassium

In the above, note there is a default index that happens to match what I
chose. The point is that if you wanted a subset including everything in a
row, you do not really need the index:

df[(df.protons > 5) & (df.protons <= 15)]

==>

	indexed	protons	moniker
3	3	7	Nitrogen
4	4	9	Fluorine
5	5	11	Sodium
6	6	13	Aluminum
7	7	15	Phosphorus

But if you did not ever need the index, you can not include it or even
suppress the default one.

My point, as I stop here from giving the entire tutorial, is for you to
think about what you want to do and consider which way to do it and then use
reasonable tools. If you really want the index so you can use it later in
various indexing ways, great. But if you just want to hold multiple items
together by an index, consider other choices including some of the above.


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of Madhavan Bomidi
Sent: Thursday, January 10, 2019 12:01 PM
To: python-list at python.org
Subject: How can I find the indices of an array with float values in python?

I have an array (numpy.ndarray) with shape (1500L,) as below:

x = array([  3.00000000e+01,   6.00000000e+01,   9.00000000e+01, ...,
         4.49400000e+04,   4.49700000e+04,   4.50000000e+04])

Now, I wanted to determine the indices of the x values between 0.0 and 15.0.
While this is simple in MATLAB or IDL by using find or where functions, I
was unable to find a best way to find the indices of all elements between
0.0 and 15.0 in the x array. Can you please suggest me a solution for the
same?
-- 
https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list