2017-08-16 48 views
0

我試圖從Api中提取數據,這應該給我某個區域的海洋條件。我有一些麻煩來拉取數據並將它們分離成單獨的變量。理想情況下,我希望這些數據以數據框的形式出現,但我不介意以另一種方式出現。我沒有這方面的經驗,所以不知道我是否正確地做到了這一點。 我的代碼:URL的Python Api將數據拉爲數據框

dataLink = 
'http://magicseaweed.com/api/MYApiKEY/forecast/?spot_id=1407&units=eu' 
data = urllib.request.urlopen(dataLink) 
data = data.readline().decode("utf-8") 
data = json.loads(data) 
data = pd.DataFrame(data) 
swell = data[(data['charts']=='swell')] 

範例顯示FORCAST:

[{"timestamp":1502755200,"localTimestamp":1502755200,"issueTimestamp":1502755200,"fadedRating":1,"solidRating":0,"swell":{"absMinBreakingHeight":0.61,"absMaxBreakingHeight":0.95,"unit":"m","minBreakingHeight":0.6,"maxBreakingHeight":0.9,"components":{"combined":{"height":1.2,"period":7,"direction":77.13,"compassDirection":"WSW"},"primary":{"height":1.2,"period":7,"direction":70.75,"compassDirection":"WSW"},"secondary":{"height":0.1,"period":11,"direction":92.74,"compassDirection":"W"}}},"wind":{"speed":18,"direction":90,"compassDirection":"W","chill":13,"gusts":25,"unit":"kph"},"condition":{"pressure":1013,"temperature":15,"weather":12,"unitPressure":"mb","unit":"c"},"charts":{"swell":"https:\/\/hist-1.msw.ms\/wave\/750\/1-1502755200-1.gif","period":"https:\/\/hist-1.msw.ms\/wave\/750\/1-1502755200-2.gif","wind":"https:\/\/hist-1.msw.ms\/gfs\/750\/1-1502755200-4.gif","pressure":"https:\/\/hist-1.msw.ms\/gfs\/750\/1-1502755200-3.gif","sst":"https:\/\/hist-1.msw.ms\/sst\/750\/1-1502755200-10.gif"}}, 
+0

你的數據不是一個格式,其中加載到數據框架中將是有益的與...合作。 –

+0

對於如何處理數據,您有什麼建議嗎?我不完全確定我在這方面做了什麼,謝謝 –

+0

嗯....你的所有數據都遵循這種格式嗎?那麼,這可能是值得的。取決於你想要誠實地做什麼。 –

回答

2

看來你需要json_normalize

from pandas.io.json import json_normalize 

data = json.loads(data) 
df = json_normalize(data) 
print (df) 

          charts.wind condition.pressure \ 
0 https:\/\/hist-1.msw.ms\/gfs\/750\/1-150275520...    1013 

    condition.temperature condition.unit condition.unitPressure \ 
0      15    c      mb 

    condition.weather ...  swell.maxBreakingHeight \ 
0     12 ...       0.9 

    swell.minBreakingHeight swell.unit timestamp wind.chill \ 
0      0.6   m 1502755200   13 

    wind.compassDirection wind.direction wind.gusts wind.speed wind.unit 
0      W    90   25   18  kph 

[1 rows x 38 columns] 
相關問題