![](/img/trans.png)
[英]Extract data from multiple files (Structure outputs) and printing to one file
[英]How to extract data from multiple SQL tables in one for multiple outputs
根據我需要從中提取數據的國家/地區,SQL DB 中有多個表。 如何從多個表中提取一個腳本中的多個數據,以在 dataframe 中使用,以基於國家/地區的多個輸出。 對於 output,如何在 dataframe 中調用此列表? 下面是我正在使用的代碼
country_name = [US,India,Canada]
query = """SELECT *
FROM CustomerDB.[Appservices\CustomerDBPowerUsers].[target_""" +country_name +"""_population];"""
df = pd.read_sql(query, engine)
建議將每個表加載到字典中。 如下所示:
country_name = ["US", "India", "Canada"] # check your list, countries must be in ""
query = """SELECT *
FROM CustomerDB.[Appservices\CustomerDBPowerUsers].[target_""" +country_name +"""_population];"""
df_dict = {} # create empty dictionary and load each table to it during the loop
for item in country_name:
df_dict[item] = df = pd.read_sql(query, engine)
稍后您可以使用字典中的數據框,如下所示:
df1 = df_dict ['US']
df2 = df_dict ['India'] # and etc.
# or directly from dict:
df_dict ['US'] = something important
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.