Я пытаюсь выучить OOP и хочу преобразовать какой-то код, который у меня есть.
Мой код:
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
# assign previous day to variable, then group and sum
prev_day = pd.read_csv('C:/Users/Name/PycharmProjects/Corona Stats/TimeSeries/03-28-2020.csv')
prev_day = prev_day.replace(np.nan, 'Other', regex=True)
prev_day = prev_day.groupby(['Country_Region']).sum()
prev_day = prev_day.reset_index()
# assign current day to variable, removed unwanted columns, then group and sum
stats_reader = pd.read_csv('C:/Users/Name/PycharmProjects/Corona Stats/TimeSeries/03-29-2020.csv')
stats_reader = stats_reader.replace(np.nan, 'Other', regex=True)
stats_clean = stats_reader.drop(['FIPS', 'Last_Update', 'Lat', 'Long_'], axis=1)
stats_clean = stats_clean.rename(columns={
'Admin2': 'County', 'Province_State': 'State', 'Country_Region': 'Country', 'Combined_Key': 'City'})
stats_clean = stats_clean.groupby(['Country']).sum()
stats_clean = stats_clean.reset_index()
# add in new columns to show difference between days
stats_clean['New Cases'] = stats_clean['Confirmed'] - prev_day['Confirmed']
stats_clean['New Deaths'] = stats_clean['Deaths'] - prev_day['Deaths']
stats_clean['New Recovered'] = stats_clean['Recovered'] - prev_day['Recovered']
stats_clean = stats_clean[[
'Country', 'Confirmed', 'New Cases',
'Deaths', 'New Deaths', 'Recovered', 'New Recovered', 'Active']]
stats_clean = stats_clean.replace(np.nan, 0, regex=True)
# calculate for global cases from previous day
prev_sum = prev_day.sum()
prev_sum['Country'] = 'World'
prev_sum = prev_sum[['Country', 'Confirmed', 'Deaths', 'Recovered']]
prev_sum = prev_sum.replace(np.nan, 0, regex=True)
# calculate for global cases for current day
sum_stats = stats_clean.sum()
sum_stats['Country'] = 'World'
sum_stats['New Cases'] = sum_stats['Confirmed'] - prev_sum['Confirmed']
sum_stats = sum_stats.replace(np.nan, 0, regex=True)
sum_stats = sum_stats[[
'Country', 'Confirmed', 'New Cases', 'Deaths', 'New Deaths', 'Recovered', 'New Recovered', 'Active']]
Моя первая попытка:
class Corona:
def __init__(self):
pass
def country_sum(self, country):
country = stats_clean['Country'].isin([country])
print(country)
Corona.country(US)
Если я сделаю этот метод stati c, он запустится, но я не использую аргумент в country_sum. Я хочу отфильтровать для любой страны пройдено. Я не знаю, как использовать аргумент в методе для фильтрации значений в столбце.
Примеры строк из исходного файла CSV:
FIPS,Admin2,Province_State,Country_Region,Last_Update,Lat,Long_,Confirmed,Deaths,Recovered,Active,Combined_Key
45001,Abbeville,South Carolina,US,2020-03-29 23:08:25,34.22333378,-82.46170658,3,0,0,0,"Abbeville, South Carolina, US"
22001,Acadia,Louisiana,US,2020-03-29 23:08:25,30.295064899999996,-92.41419698,9,1,0,0,"Acadia, Louisiana, US"
51001,Accomack,Virginia,US,2020-03-29 23:08:25,37.76707161,-75.63234615,3,0,0,0,"Accomack, Virginia, US"
16001,Ada,Idaho,US,2020-03-29 23:08:25,43.4526575,-116.24155159999998,92,1,0,0,"Ada, Idaho, US"
19001,Adair,Iowa,US,2020-03-29 23:08:25,41.33075609,-94.47105874,1,0,0,0,"Adair, Iowa, US"