I have a CSV containing weather data like max and min temperatures, precipitation, longitude and latitude of the weather stations etc. Each category of data is stored in a single column.
I want to find the location of the maximum and minimum temperatures. Finding the max or min is easy: numpy.min(my_temperatures_column)
How can I find the position of where the min or max is located, so I can find the latitude and longitude?
Here is my attempt:
def coldest_location(data): coldest_temp= numpy.min(mean_temp) for i in mean_temp: if mean_temp[i] == -24.6: print i
Error: List indices must be int
I saved each of the columns of my CSV into variables, so they are all individual lists.
lat = [row[0] for row in weather_data] # latitude long = [row[1] for row in weather_data] # longitude mean_temp = [row[2] for row in weather_data] # mean temperature
I have resolved the problem as per the suggestion list.index(x)
mean_temp.index(coldest_temp) coldest_location=[long[5],lat[5]]
Unsure if asking a second question within a question is proper, but what if there are two locations with the same minimum temperature? How could I find both and their indices?
In summary, the index() function is the easiest way to find the position of an element within a Python list. Although, this function only returns the index of the first occurrence of the given value.
To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found. The following illustrates the syntax of the indexOf() method.
Have you thought about using Python list's .index(value)
method? It return the index in the list of where the first instance of the value
passed in is found.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With