循环以将多个列表附加到dataframe python中(Loop to append multiple lists into dataframe python)

编程入门 行业动态 更新时间:2024-10-27 02:25:42
循环以将多个列表附加到dataframe python中(Loop to append multiple lists into dataframe python)

我必须使用返回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')

更多推荐

本文发布于:2023-08-01 15:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1360111.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   列表   dataframe   python   lists

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!