繁体   English   中英

遍历 python 和 pandas 循环

[英]Iterating through the python and pandas loop

查看循环如何在迭代中工作的最佳方法是什么? 我已经定义了 2 个函数,它们必须一个接一个地达到 go(第二个得到第一个的结果并完成它)。

最终我需要 2 线 pandas dataframe 作为 output。

下面的示例代码。

def candle_data (
    figi, 
    int1 = candle_resolution,
    from1 = previous_minutemark,
    to1 = last_minutemark
    ):
    response = market_api.market_candles_get(figi = ticker_figi_test, from_ = from1, to = to1, interval = int1)
    if response.status_code == 200:
        return response.parse_json().dict()
    else:
        return print(response.parse_error())

def response_to_pandas_df (response):
    df_candles = pd.DataFrame(response['payload'])
    df_candles = pd.json_normalize(df_candles['candles'])
    df_candles = df_candles[df_candles['time'] >= previous_minutemark]
    df_candles = df_candles[['c', 'figi','time']]
    df_candles_main = df_candles_template.append(df_candles)
    return df_candles_main

然后我在循环中调用函数:

ticker_figi_list = ["BBG000CL9VN6", "BBG000R7Z112"]

df_candles_main = df_candles_template

for figi in ticker_figi_list:
    response = candle_data(figi)
    df_candles_main = response_to_pandas_df(response)

但作为回报,我只得到列表中第一个 FIGI 的一行数据。

我想,我用仅包含 1 个值的figi_ticker_test定义了candle_data() function 可能就是这种情况。 但我不确定如何解决这个问题。

先感谢您。

看起来问题是您正在使用figi = ticker_figi_test 我假设 ticket_figi_test 等于您列表中的第一个数字,因此您实际上并没有在每次迭代中使用不同的数字调用 api。 尝试更改为以下内容:

response = market_api.market_candles_get(figi = figi, from_ = from1, to = to1, interval = int1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM