-1

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)

enter image description here

if loop and for loop but I have an error as an output

1 Answer 1

1

I would do this in the following way:

import pandas as pd
import numpy as np

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'),
]

def to_code_range(valeur): 
    if type(valeur) != int: 
        print("Pas un int")
        return np.nan
    for code_from, code_to, name in mappings:
        if code_from <= valeur <= code_to:
            return name
    return np.nan

# Assuming 'report' is a DataFrame with a column 'SicCodes'
report = pd.DataFrame({
    'SicCodes': [1000, 15000, 20000, 40000, 50000, 60000, 70000, 91000, 98000]
})

report['SICCode.SicText_1'] = report['SicCodes'].apply(to_code_range)

print(report)

Output of interpreter

   SicCodes      SICCode.SicText_1
0      1000            Agriculture
1     15000           Construction
2     20000          Manufacturing
3     40000       Utility Services
4     50000        Wholesale Trade
5     60000             Financials
6     70000               Services
7     91000  Public Administration
8     98000        Nonclassifiable
1
  • Thanks for your time answering it, it looks legit but i basically did an overall data wrangling to make sure it fits.
    – Florian
    Commented Mar 20 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.