1

I am trying to create a new column, not with the existing values of the current column, but with new values. I went ahead and created a function to do this, however, I am getting an error. The result I want is each municipality to have a code number i.e TO =1, SC=2 etc.

def ET(ET):
    return "ET" == 1
def TO(TO):
    return "TO" == 2
def NY(NY):
    return "NY" == 3
def SC(SC):
    return "SC" == 4
def coded_municipality(row):
    if row['coded_municipality'] == "ET" :
        return 1
    if row['coded_municipality'] == "NY" :
        return 2
    if row['coded_municipality'] == "SC" :
        return 3
    if row['coded_municipality'] == "TO" :
        return 4

marriage_data['Code'] = marriage_data.apply(lambda row : coded_municipality(row),axis=1)

However, I am still getting an error saying 'coded_municipality', 'occurred at index 0'

What am I doing wrong?

1 Answer 1

2

You can use map.

d = {"ET": 1, "TO": 2, "NY": 3, "SC": 4}
marriage_data['Code'] = marriage_data['coded_municipality'].map(d)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.