Reputation: 11
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?
Upvotes: 1
Views: 106
Reputation: 38415
You can use map.
d = {"ET": 1, "TO": 2, "NY": 3, "SC": 4}
marriage_data['Code'] = marriage_data['coded_municipality'].map(d)
Upvotes: 2