我需要从API中提取一些列。我试试:
#importing requests
import requests as re
#importing csv
import csv
#importing pandas
import pandas as pd
#taking url and asigning to url variable
url="https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2016-10-01&endtime=2016-10-02"
#assigning to data after getting the url
data=re.get(url)
#put it in the eq variable
eq=data.json()
#reult we can sse here
eq['features']
def obtain_data(eq):
i=0
print('Lat\tLongitude\tTitle\tPlace\tMag')
while i < len(eq['features']):
print(str(eq['features'][i]['geometry']['coordinates'][0])+'\t'+str(eq['features'][i]['geometry']['coordinates'][1])+'\t'+str(eq['features'][i]['properties']['title'])+'\t'+str(eq['features'][i]['properties']['place']+'\t'+str(eq['features'][i]['properties']['mag'])))
i=i+1
final_data= obtain_data(eq)
我需要将coordinates
拆分为2列 - Lat
和Longitude
,并提取列Title
,Place
和\Mag
。输出为csv
,tab
分隔符。
答案 0 :(得分:0)
我认为你需要:
from pandas.io.json import json_normalize
#extract data
df = json_normalize(data['features'])
#get first and second values of lists
df['Lat'] = df['geometry.coordinates'].str[0]
df['Longitude'] = df['geometry.coordinates'].str[1]
#rename original columns names
df = df.rename(columns={'properties.title':'Title',
'properties.place':'Place',
'properties.mag':'Mag'})
#filter only necessary columns
df = df[['Lat','Longitude', 'Title','Place','Mag']]
print (df.head())
Lat Longitude Title \
0 -118.895700 38.860700 M 1.0 - 27km ESE of Yerington, Nevada
1 -124.254833 40.676333 M 2.5 - 7km SW of Humboldt Hill, California
2 -116.020000 31.622500 M 2.6 - 53km ESE of Maneadero, B.C., MX
3 -121.328167 36.698667 M 2.1 - 13km SSE of Ridgemark, California
4 -115.614500 33.140500 M 1.5 - 10km W of Calipatria, CA
Place Mag
0 27km ESE of Yerington, Nevada 1.00
1 7km SW of Humboldt Hill, California 2.52
2 53km ESE of Maneadero, B.C., MX 2.57
3 13km SSE of Ridgemark, California 2.06
4 10km W of Calipatria, CA 1.45
#write to file
df.to_csv(file, sep='\t', index=False)