refactoring

This commit is contained in:
frankknoll
2023-02-07 11:37:43 +01:00
parent d7e52bdf49
commit 3f0b650519
2 changed files with 31 additions and 24 deletions

View File

@@ -1,10 +1,10 @@
import pycountry
import pandas as pd
from Splttype2CountryConverter import Splttype2CountryConverter
class CountryColumnAdder:
def __init__(self, dataFrame_SPLTTYPE_By_VAERS_ID):
self.dataFrame_COUNTRY_By_VAERS_ID = self._create_dataFrame_COUNTRY_By_VAERS_ID(dataFrame_SPLTTYPE_By_VAERS_ID)
self.dataFrame_COUNTRY_By_VAERS_ID = Splttype2CountryConverter.convertSplttype2Country(dataFrame_SPLTTYPE_By_VAERS_ID)
def addCountryColumn(self, dataFrame):
return pd.merge(
@@ -14,24 +14,3 @@ class CountryColumnAdder:
left_index = True,
right_index = True)
def _create_dataFrame_COUNTRY_By_VAERS_ID(self, dataFrame_SPLTTYPE_By_VAERS_ID):
dataFrame_COUNTRY_By_VAERS_ID = dataFrame_SPLTTYPE_By_VAERS_ID[['SPLTTYPE']].copy()
dataFrame_COUNTRY_By_VAERS_ID['COUNTRY'] = self._splttype2Country(dataFrame_COUNTRY_By_VAERS_ID['SPLTTYPE'])
dataFrame_COUNTRY_By_VAERS_ID = dataFrame_COUNTRY_By_VAERS_ID.drop(columns = ['SPLTTYPE'])
return dataFrame_COUNTRY_By_VAERS_ID
def _splttype2Country(self, splttypeSeries):
return (splttypeSeries
.apply(
lambda splttype:
self._getCountryNameOfSplttypeOrDefault(
splttype = splttype,
default = 'Unknown Country'))
.astype("string"))
def _getCountryNameOfSplttypeOrDefault(self, splttype, default):
if not isinstance(splttype, str):
return default
country = pycountry.countries.get(alpha_2 = splttype[:2])
return country.name if country is not None else default

View File

@@ -0,0 +1,28 @@
import pycountry
class Splttype2CountryConverter:
@staticmethod
def convertSplttype2Country(dataFrame_SPLTTYPE_By_VAERS_ID):
dataFrame_COUNTRY_By_VAERS_ID = dataFrame_SPLTTYPE_By_VAERS_ID[['SPLTTYPE']].copy()
dataFrame_COUNTRY_By_VAERS_ID['COUNTRY'] = Splttype2CountryConverter._splttype2Country(dataFrame_COUNTRY_By_VAERS_ID['SPLTTYPE'])
dataFrame_COUNTRY_By_VAERS_ID = dataFrame_COUNTRY_By_VAERS_ID.drop(columns = ['SPLTTYPE'])
return dataFrame_COUNTRY_By_VAERS_ID
@staticmethod
def _splttype2Country(splttypeSeries):
return (splttypeSeries
.apply(
lambda splttype:
Splttype2CountryConverter._getCountryNameOfSplttypeOrDefault(
splttype = splttype,
default = 'Unknown Country'))
.astype("string"))
@staticmethod
def _getCountryNameOfSplttypeOrDefault(splttype, default):
if not isinstance(splttype, str):
return default
country = pycountry.countries.get(alpha_2 = splttype[:2])
return country.name if country is not None else default