我必须使用返回json对象的YQL从yahoo requests.get()两个url。 我正在收回一个json对象,我将其存储到list() 。 然后我循环解析数据并创建一个dic然后创建一个pandas数据框。 发生只有一个列表被附加到数据框。 似乎在上一次迭代中,第二个列表覆盖了第一个列表。 此时,我无法弄清楚如何迭代列表以append()列表的两个元素。 这是我的代码......
import requests import pandas as pd urls = ['https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22DIA%22%2C%22SPY%22%2C%22IWN%22)%20and%20startDate%20%3D%20%222015-01-01%22%20and%20endDate%20%3D%20%222015-10-31%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22DIA%22%2C%22SPY%22%2C%22IWN%22)%20and%20startDate%20%3D%20%222015-11-01%22%20and%20endDate%20%3D%20%222016-08-31%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback='] for url in urls: data = requests.get(url) data_json = data.json() quote_list = [] for quote in data_json['query']['results']['quote']: quote_dic = {'symbol': quote['Symbol'], 'date': quote['Date'], 'volume': quote['Volume'], 'low': quote['Low'], 'high': quote['High'], 'open': quote['Open'], 'close': quote['Close'], 'adj_close': quote['Adj_Close']} quote_list.append(quote_dic) quote_df = pd.DataFrame(quote_list) quote_df.to_csv('stocks.csv')我需要能够将整个list()附加到数据框中。 这段代码的修复方法是什么?
I have to requests.get() two urls from yahoo utilizing YQL that returns a json object. I'm getting back a json objects that I store into a list(). Then I'm looping to parse the data and creating a dic to then create a pandas data frame. Happened that only one list is getting appended to the data frame. Seems like in the last iteration, the 2nd list overwrites the first list. At this point, I can't figure out how to iterate on the list to append() both elements of the list. Here is my code...
import requests import pandas as pd urls = ['https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22DIA%22%2C%22SPY%22%2C%22IWN%22)%20and%20startDate%20%3D%20%222015-01-01%22%20and%20endDate%20%3D%20%222015-10-31%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22DIA%22%2C%22SPY%22%2C%22IWN%22)%20and%20startDate%20%3D%20%222015-11-01%22%20and%20endDate%20%3D%20%222016-08-31%22&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback='] for url in urls: data = requests.get(url) data_json = data.json() quote_list = [] for quote in data_json['query']['results']['quote']: quote_dic = {'symbol': quote['Symbol'], 'date': quote['Date'], 'volume': quote['Volume'], 'low': quote['Low'], 'high': quote['High'], 'open': quote['Open'], 'close': quote['Close'], 'adj_close': quote['Adj_Close']} quote_list.append(quote_dic) quote_df = pd.DataFrame(quote_list) quote_df.to_csv('stocks.csv')I need to be able to append the entire list() into the data frame. What would be the fix for this code?
最满意答案
只需创建一个数据帧列表,并在循环结束时将它们连接起来:
df_list = [] for url in urls: data = requests.get(url) data_json = data.json() df = pd.DataFrame(data_json['query']['results']['quote']) df_list.append(df) quote_df = pd.concat(df_list) quote_df.to_csv('stocks.csv')Just create a list of dataframes, and concat them at the end of the loop:
df_list = [] for url in urls: data = requests.get(url) data_json = data.json() df = pd.DataFrame(data_json['query']['results']['quote']) df_list.append(df) quote_df = pd.concat(df_list) quote_df.to_csv('stocks.csv')更多推荐
发布评论