application_vnd.jupyter.stderr_output_1.png
import pandas as pd

df_train = pd.read_csv('prediction_challenge/covid19-global-forecasting-week-4/train.csv')

test_province_country_groups = df_train.groupby(['Province_State', 'Country_Region'])
province_country_groups = df_train.groupby(['Province_State', 'Country_Region'])

df_train.isnull().sum()

df_train.loc[df_train['Province_State'].isnull(), 'Province_State'] = 'None'
df_train.isnull().sum()

province_country_groups = df_train.groupby(['Province_State', 'Country_Region'])

province_country_groups = df_train.groupby(['Province_State', 'Country_Region'])
confirmed_cases_list = []
for p_c in province_country_groups.groups.keys():
    confirmed_cases_list = province_country_groups.get_group(p_c)['ConfirmedCases'].tolist()
    corrected = False
    for i in range(len(confirmed_cases_list) - 1):
        if confirmed_cases_list[i] > confirmed_cases_list[i + 1]:
            try:
                if confirmed_cases_list[i] <= confirmed_cases_list[i + 2]:
                    confirmed_cases_list[i + 1] = confirmed_cases_list[i]
                elif confirmed_cases_list[i - 1] <= confirmed_cases_list[i + 1]:
                    confirmed_cases_list[i] = confirmed_cases_list[i - 1]
            except IndexError:
                confirmed_cases_list[i + 1] = confirmed_cases_list[i]
            corrected = True
    if corrected == True:
        df_train.loc[(df_train['Country_Region'] == p_c[1]) & (df_train['Province_State'] == p_c[0]), 'ConfirmedCases'] = confirmed_cases_list

date_range = df_train['Date']
day_groups = df_train.groupby('Date')
latest = day_groups.get_group(max(date_range))