【问题标题】:API result (COMPLEX NESTED) into Data Frame PandaAPI 结果(复杂嵌套)到 Data Frame Panda
【发布时间】:2018-08-08 03:05:38
【问题描述】:

我需要从 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 列 - LatLongitude 并提取列 TitlePlace\Mag。输出为 csvtab 分隔符。

【问题讨论】:

  • 有点问题修改。可以吗?我的解决方案如何运作?

标签: python json pandas csv dictionary


【解决方案1】:

我认为你需要:

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)

【讨论】:

  • 输出只有 5 行,尽管在功能中有 306 行......!
  • @KaushalKumarChawda - 这是因为 df.head() 只返回前 5 行,测试它print (df) - 它重新运行所有数据
  • 非常感谢,它真的鼓舞了我,现在我要研究这个 json_normalize 以及如何使用它。它非常有帮助:) :)
猜你喜欢
  • 2018-04-03
  • 1970-01-01
  • 2021-02-21
  • 2022-12-31
  • 1970-01-01
  • 2020-06-12
  • 2019-06-06
  • 1970-01-01
  • 2020-02-20
相关资源
最近更新 更多