如何在Python&;中使用for循环连接数据帧;熊猫

如何在Python&;中使用for循环连接数据帧;熊猫,python,pandas,dataframe,dictionary,concatenation,Python,Pandas,Dataframe,Dictionary,Concatenation,我有一个数据帧字典(248个国家),我想把它合并成一个数据帧 该数据帧称为dfs,因此如果我想访问该数据帧的内容,请使用: dfs["Albania"] 我在前面学习如何合并数据帧时,使用了下面的代码来处理4个数据帧 我是否可以将其调整为我现在想要包括的248个国家的循环,并将每个连接的df的键设置为国家名称 在过去的几个小时里,我在这方面进展甚微 datasets = [df_ireland, df_italy, df_france, df_germany] frames = [] fo

我有一个数据帧字典(248个国家),我想把它合并成一个数据帧

该数据帧称为dfs,因此如果我想访问该数据帧的内容,请使用:

dfs["Albania"]
我在前面学习如何合并数据帧时,使用了下面的代码来处理4个数据帧

我是否可以将其调整为我现在想要包括的248个国家的循环,并将每个连接的df的键设置为国家名称

在过去的几个小时里,我在这方面进展甚微

datasets = [df_ireland, df_italy, df_france, df_germany]

frames = []

for frame in datasets:
    frames.append(frame)

df_join = pd.concat(frames, keys=['Ireland', 'Italy', 'France', 'Germany'])
以下是我用来构建字典的循环,以防有任何好处:

# Import the libraries
import requests
import requests_cache

import json

import pandas as pd
import numpy as np
from pandas import Series, DataFrame, json_normalize

from datetime import datetime

# Make an API call and store the response.
sum_url = 'https://api.covid19api.com/summary'
sum_data = requests.get(sum_url)

# Store the API response in a variable.
available_sum_data = sum_data.json()
sum_df = json_normalize(available_sum_data["Countries"])

# Make a list of countries
countries = sum_df['Country'].tolist()

# Make a empty dictionary to hold dataframes
dfs = {}


for country in countries:
    print(country)

    try:
        # check the cache and if old data call api
        requests_cache.install_cache(f'{country} cache', expire_after=21600)
        url = f'https://api.covid19api.com/total/dayone/country/{country}'
        data = requests.get(url)

        # test if cache used
        print(data.from_cache)

    except requests.exceptions.RequestException as e:  # This is the correct syntax
        print(e)
        print('cant print' + country)

    try:
        available_data = data.json()
        dfs[f'{country}'] = pd.json_normalize(available_data)


        # Create Daily new cases column & SMA
        dfs[f'{country}']["New Cases"] = dfs[f'{country}']['Confirmed'].diff()
        dfs[f'{country}']["SMA_10 New Cases"] = dfs[f'{country}']["New Cases"].rolling(window=10).mean()

        # Create Daily new deaths column & SMA
        dfs[f'{country}']["New Deaths"] = dfs[f'{country}']['Deaths'].diff()
        dfs[f'{country}']["SMA_10 New Deaths"] = dfs[f'{country}']["New Deaths"].rolling(window=10).mean()


    except:
        print('cant format to json: ' + country)

我想你已经有了很棒的字典
dfs
,所以你不需要做循环。你能试试这个吗

df_joined = pd.concat(dfs.values(), keys=dfs.keys())

哇!那太完美了。非常感谢。