import pandas as pd import numpy as np data = pd.read_csv('owid-covid-data.csv') df = data[['iso_code', 'date', 'total_cases']] df['date']=pd.to_datetime(df['date'], format='%Y-%m-%d') df.info() iso = df.iso_code.unique() df2 = pd.DataFrame(columns=['iso_code', 'date', 'total_cases']) df3 = pd.DataFrame(columns=['iso_code', 'date', 'total_cases']) for country in iso: country_v = df.loc[df['iso_code'] == country] months = country_v.set_index('date').groupby(pd.Grouper(freq='M')).max() i=0 for index, month in months.iterrows(): #df2.append(month) if i==months.shape[0]-1: df3 = df3.append({'iso_code': month['iso_code'], 'date': month.name, 'total_cases': month['total_cases']}, ignore_index=True) df2 = df2.append({'iso_code': month['iso_code'], 'date': month.name, 'total_cases': month['total_cases']}, ignore_index=True) i += 1 df2.to_csv('month-covid.csv', sep=',') df3.to_csv('max-covid.csv', sep=',')