Files
HowBadIsMyBatch/src/CountryColumnAdder.py
frankknoll e87fe0c8ba refactoring
2022-11-22 12:40:26 +01:00

25 lines
831 B
Python

import pycountry
class CountryColumnAdder:
@staticmethod
def addCountryColumn(dataFrame):
dataFrame['COUNTRY'] = CountryColumnAdder.getCountryColumn(dataFrame)
return dataFrame.astype({'COUNTRY': "string"})
@staticmethod
def getCountryColumn(dataFrame):
return dataFrame.apply(
lambda row:
CountryColumnAdder._getCountryNameOfSplttypeOrDefault(
splttype = row['SPLTTYPE'],
default = 'Unknown Country'),
axis = 'columns')
@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