I've been reviewing data for a project on jupyter and I was hoping to map some data to fit it in a specific category of company.
In the end, I used a big if loop but the problem as expected is I cant parse each individual cells using a column so is there any other better way to do it? the code I got wasn't functional in the first place so I tried to improve it with my little knowledge of python.
So I wanna pick a value in SicCodes column compare it into the mapping and basically get the name as an output. I thought as a first approach to get my most simple parsing of data with if loop and then improve it later on. But indeed, I can't shove data frame into my little to_code_range so I thought about doing it with a for loop, but isn't successful at the moment.
Would anyone have a good idea on how to improve that?
mappings = [
(1000, 9990, 'Agriculture'),
(10000, 14990, 'Mining'),
(15000, 17990, 'Construction'),
(18000, 19990, 'not used'),
(20000, 39990, 'Manufacturing'),
(40000, 49990, 'Utility Services'),
(50000, 51990, 'Wholesale Trade'),
(52000, 59990, 'Retail Trade'),
(60000, 69200, 'Financials'),
(70000, 90040, 'Services'),
(91000, 97290, 'Public Administration'),
(98000, 99990, 'Nonclassifiable'),
]
"""errors = set()
def to_code_range(i):
if type(i) != int:
print("Pas un int")
if i=="None Supplied":
return np.nan
code = int(i)
for code_from, code_to, name in mappings:
if (code<=code_to)&(code>=code_from):
return name
errors.add(code)
return np.nan"""
def to_code_range(valeur):
if type(valeur) != int: print("Pas un int")
code = int(valeur)
if (code<1000): return np.nan
if (code>=1000)&(code<=9990): return "Agriculture"
if (code>=10000)&(code<=14990): return "Mining"
if (code>=10000)&(code<=14990): return "Mining"
if (code>=15000)&(code<=17990): return "Construction"
if (code>=18000)&(code<=19990): return "not used"
if (code>=20000)&(code<=39990): return "Manufacturing"
if (code>=40000)&(code<=49990): return "Utility Services"
if (code>=50000)&(code<=51990): return "Wholesale Trade"
if (code>=52000)&(code<=59990): return "Retail Trade"
if (code>=60000)&(code<=69200): return "Financials"
if (code>=70000)&(code<=90040): return "Services"
if (code>=91000)&(code<=97290): return "Public Administration"
if (code>=98000)&(code<=99990): return "Nonclassifiable"
else :return np.nan
#report['SICCode.SicText_1'] = to_code_range(report["SicCodes"])
for i in report['SicCodes']: report['SICCode.SicText_1'][i] = to_code_range(i)
if loop and for loop but I have an error as an output