0

This is the URL i'm using:

http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.csv

And I need to output it like this:

Downloading earthquake data from USGS ... Largest magnitude quake is: Time: 2016-10-17T06:14:58.370Z Latitude: -6.0526 Longitude: 148.8617 Location: 78km WNW of Kandrian, Papua New Guinea Magnitude: 6.9 Depth: 35

I already have a function that reads and decodes the lines this is a bit of the code:

def online_display_largest_quake():
 print('Downloading earthquake data from USGS ...')

 earthquakes = get_text_lines_from_url('http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.csv')
 print (earthquakes)
 best_mag = 0
 best_item = []

 for (item) in earthquakes[1:]:

    if float(item[4]) > best_mag:
        best_mag = float(str(item[4]))
        best_item = item

 earthquake_output(best_item) 
m.orchard
  • 11
  • 1
  • 1
    Is `item` the variable you output (and thus, is `item[4]` the data in the 5th column of the row contained in item)? – Stephen B Nov 23 '16 at 20:57
  • @PyNoob its a bunch of data concerning different earthquakes taken from an online excel document. I want to pick out the one with the highest magnitude and then output the full row in a readable format. So item[4] refers to the magnitude column but it is actually refering to the 5th character which is '-' this is where im stuck – m.orchard Nov 23 '16 at 20:59
  • 1
    Maybe you can provide the structure of `item` and more code for a more tailored answer. In the meantime, I provided one that uses pandas. – Stephen B Nov 23 '16 at 21:11
  • @PyNoob Ive edited the question to give a context – m.orchard Nov 23 '16 at 21:18

1 Answers1

0

Perhaps you can post a few lines of the CSV you're using and some more code to give us a better idea of the data and how you're approaching its analysis.

I found a CSV online that contains rows of earthquake data that I will use for my example. With pandas, you can directly input a URL and easily get the row where the maximum magnitude earthquake occurs (which, I believe, is what you're doing).

> import pandas as pd

> url = 'http://itp.nyu.edu/~cm2897/blog/wp-content/uploads/2012/03/global-earthquakes.csv'
> df = pd.read_csv(url)
> df.head()
   year  month  day      time  latitude  longitude  magnitude  depth
0  1973      1    1   34609.8     -9.21     150.63        5.3     41
1  1973      1    1   52229.8    -15.01    -173.96        5.0     33
2  1973      1    1  114237.5    -35.51     -16.21        6.0     33
3  1973      1    2    5320.3     -9.85     117.43        5.5     66
4  1973      1    2   22709.2      1.03     126.21        5.4     61

> df.loc[df['magnitude'].idxmax()]
year         2004.00
month          12.00
day            26.00
time         5853.45
latitude        3.30
longitude      95.98
magnitude       9.00
depth          30.00
Name: 48506, dtype: float64

Pandas' Series.idxmax method returns the index where the maximum value occurs in a Series (in this case, the magnitude column from the DataFrame). See this answer for more information. With this index, we can use DataFrame.loc to return the corresponding row.

Community
  • 1
  • 1
Stephen B
  • 1,246
  • 1
  • 10
  • 23