如何在Python和Pandas中使用for循环连接数据帧

时间:2020-05-08 19:47:39

标签: python pandas dataframe dictionary concatenation

我有一个数据框字典(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)

1 个答案:

答案 0 :(得分:1)

我认为您已经拥有出色的字典dfs,因此您无需执行循环。你可以试试这个吗?

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