से पांडा डेटाफ्रेम बनाना मैं जनगणना डेटा पुनर्प्राप्त करने के लिए एक एपीआई बना रहा हूं, लेकिन मुझे आउटपुट स्वरूपण में परेशानी हो रही है। मेरा प्रश्न वास्तव में दो में से एक है:एपीआई कॉल
1) मैं अपने API कॉल (आदर्श एक dataframe) कैसे सुधार कर सकते हैं, जिससे कि उत्पादन खूबसूरत है
या
2) मैं कैसे सूची में हेरफेर कर सकते हैं कि मुझे वर्तमान में ऐसा लगता है कि यह एक पांडा डेटाफ्रेम में है?
import requests
import pandas as pd
import numpy as np
mytoken = "numbersandletters"
# this is my API key, so unfortunately I can't provide it
def state_data(token, variables, year = 2010, state = "*", survey = "sf1"):
state = [str(i) for i in state]
# make sure the input for state (integers) are strings
variables = ",".join(variables) # squish all the variables into one string
year = str(year)
combine = ["http://api.census.gov/data/", year, "/", survey, "?key=", mytoken, "&get=", variables, "&for=state:"]
# make a list of all the components to construct a URL
incomplete_url = "".join(combine) # the URL without the state tackd on to the end
complete_url = map(lambda i: incomplete_url + i, state) # now the state is tacked on to the end; one URL per state or for "*"
r = []
r = map(lambda i: requests.get(i), complete_url)
# make an API call to each complete_url
data = map(lambda i: i.json(), r)
print r
print data
print type(data)
df = pd.DataFrame(data)
print df
फ़ंक्शन को कॉल का एक उदाहरण यह है, नीचे दिए गए उत्पादन के साथ:
यहाँ मैं अब तक है।
state_data(token = mytoken, state = [47, 48, 49, 50], variables = ["P0010001", "P0010001"])
में जिसके परिणामस्वरूप:
[<Response [200]>, <Response [200]>, <Response [200]>, <Response [200]>]
[[[u'P0010001', u'P0010001', u'state'], [u'6346105', u'6346105', u'47']],
[[u'P0010001', u'P0010001', u'state'], [u'25145561', u'25145561', u'48']],
[[u'P0010001', u'P0010001', u'state'], [u'2763885', u'2763885', u'49']],
[[u'P0010001', u'P0010001', u'state'], [u'625741', u'625741', u'50']]]
<type 'list'>
0 1
0 [P0010001, P0010001, state] [6346105, 6346105, 47]
1 [P0010001, P0010001, state] [25145561, 25145561, 48]
2 [P0010001, P0010001, state] [2763885, 2763885, 49]
3 [P0010001, P0010001, state] [625741, 625741, 50]
जबकि वांछित परिणाम होगा:
P0010001 P0010001 state
0 6346105 6346105 47
1 25145561 25145561 48
2 2763885 2763885 49
3 625741 625741 50
Fwiw, आर में अनुरूप कोड के नीचे है। मैं एक पुस्तकालय मैं अजगर को आर में लिखी गई जानकारी का अनुवाद कर रहा हूँ:
state.data = function(token, state = "*", variables, year = 2010, survey = "sf1"){
state = as.character(state)
variables = paste(variables, collapse = ",")
year = as.character(year)
my.url = matrix(paste("http://api.census.gov/data/", year, "/", survey, "?key=", token,
"&get=",variables, "&for=state:", state, sep = ""), ncol = 1)
process.url = apply(my.url, 1, function(x) process.api.data(fromJSON(file=url(x))))
rbind.dat = data.frame(rbindlist(process.url))
rbind.dat = rbind.dat[, c(tail(seq_len(ncol(rbind.dat)), 1), seq_len(ncol(rbind.dat) - 1))]
rbind.dat
}
क्या आप एक उदाहरण दे सकते हैं कि मूल डेटा कैसा दिखता है (जिसे आप 'full_url' से पुनर्प्राप्त करते हैं? यदि यह जेसन है, हो सकता है कि आप 'pd.read_json' का उपयोग कर सकें? – joris
मैंने एक' प्रिंट आर 'और आउटपुट जोड़ा। आर एक सूची है। – Nancy