简体   繁体   English

有没有办法循环通过 python 中的 function?

[英]Is there a way to loop through a function in python?

I have this code which pulls data from the Federal Reserve, through their API.我有这段代码,它通过他们的 API 从美联储提取数据。 I am trying to figure out how to have different FRED ID's that can plug into the function and then output to a data table.我试图弄清楚如何将不同的 FRED ID 插入 function 和 output 到数据表中。 So, for example, I would input "CPIAUCSL" and then it would give me a list of values, then the script would input "A191RL1Q225SBEA" and then append those values to the bottom of the data table (so that the values of CPIAUCSL would be above it).因此,例如,我会输入“CPIAUCSL”,然后它会给我一个值列表,然后脚本会输入“A191RL1Q225SBEA”,然后 append 将这些值输入到数据表的底部(这样 CPIAUCSL 的值将高于它)。 I tried to do this with the below script, but it would just overwrite the previous values.我尝试使用以下脚本执行此操作,但它只会覆盖以前的值。 Any ideas?有任何想法吗?

import pandas as pd  
from requests import request  
import json  
from requests import request  
from urllib.request import urlopen  
from pandas import ExcelWriter  

def fred_variables(fred_id_list):  
    main_url= "https://api.stlouisfed.org/fred/series/observations?series_id="  
    API= "&api_key=50ebfb4929a3fe603e3b369d51826822"  
    file= "&file_type=json"  
    descending= "&sort_order=desc"  
    max_return= "&limit=21"  
    final_URL= main_url + fred_id_list + API + file + descending + max_return
    page = urlopen(final_URL)  
    data1= page.read()  
    nested_json = json.loads(data1)  
    json_file = pd.DataFrame.from_dict(pd.json_normalize(nested_json, record_path="observations"))  
    df= json_file  
    df['fredID'] = str(fred_id_list)  
    df2 = df.drop(df.columns[[0, 1]], axis=1)  
    return df2  

    fred_id_list=["CPIAUCSL","A191RL1Q225SBEA"]  
    for x in fred_id_list:  
        print(x)  

fred_variables(x)  
```[enter image description here][1]


  [1]: https://i.stack.imgur.com/8Tglt.png

I created a fake scenario for you:我为您创建了一个假场景:

data = {
    'CPIAUCSL': [12, 23, 1, 43, 12, 53, 13, 53, 1, 53, 123, 534],
    'A191RL1Q225SBEA': [65, 87, 23, 7, 23, 765, 23]
}

def fred_variables(fred_id):
    # processing code
    fake_result = data[fred_id]
    return fake_result

Consider the function is your function.考虑 function 是您的 function。 I'm assuming you return a list of items.我假设您返回一个项目列表。 You can utilize Python's map function for your use-case.您可以将 Python 的map function 用于您的用例。 Like:喜欢:

fred_id_list = ["CPIAUCSL","A191RL1Q225SBEA"]
result = list(map(fred_variables, fred_id_list))
print(result)

It will generate following output:它将生成以下 output:

[[12, 23, 1, 43, 12, 53, 13, 53, 1, 53, 123, 534], [65, 87, 23, 7, 23, 765, 23]]

If you with the combine this whole data into one list, u can do the following:如果您将整个数据合并到一个列表中,您可以执行以下操作:

final_result = []
for res in result:
    final_result.extend(res)

print(final_result)

The final output will be like:最终的 output 将如下所示:

[12, 23, 1, 43, 12, 53, 13, 53, 1, 53, 123, 534, 65, 87, 23, 7, 23, 765, 23]

For you specific case对于您的具体情况

Now coming to your question, you are returning a Pandas DataFrame from your function.现在来回答您的问题,您将从 function 返回 Pandas DataFrame。 So, what you need to do:所以,你需要做的是:

fred_id_list = ["CPIAUCSL","A191RL1Q225SBEA"]
result = list(map(fred_variables, fred_id_list))

Here result will be a list of DataFrames.这里的result将是一个 DataFrames 列表。 You can combine all these DataFrames with the help of Pandas' concat function.您可以在 Pandas 的concat function 的帮助下组合所有这些 DataFrame。

final_result = pd.concat(result)

I hope it helps.我希望它有所帮助。 Thanks.谢谢。

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

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