Is my apply lambda wrong? Im so new to python and trying to fix a previous developers code on an important project

Viewed 43

Can anyone help explain why this isnt working properly?

Im not as experienced with Python as Id like and working through another previous developers code, this is the cell:

#Move zipcodes to new column
rawdata['zipcode']=rawdata.apply(lambda x: x['input_value'] if x['question_id']==92 else 
None,axis=1)

#Move zipcode data to new table
zipcodes=rawdata[['respondent','zipcode']]
zipcodes=zipcodes.where(zipcodes['zipcode'].notna()).dropna()

#Store zipcodes in dict
zipcodes=zipcodes.set_index('respondent').to_dict()['zipcode']

#Add leading zeroes removed
for i in zipcodes:
zipcodes[i]= str(zipcodes[i]) if len(str(zipcodes[i]))>4 else (5- 
len(str(zipcodes[i])))*"0"+str(zipcodes[i])

#Merge zipcode data in
data['zipcode']=data['respondent'].apply(lambda x: zipcodes[x])

Apologies the format isn't the best.

Im attaching screenshots as well from Jupyter. Seems like the apply lamba/zipcodes X is causing the issue and Im not quite understanding why.

#Merge zipcode data in data['zipcode']=data['respondent'].apply(lambda x: zipcodes[x])

I havent loaded up any of the zipcodes or anything into the file from another csv yet either. Am i missing something with Python? Sorry if this is oblivious yall.

JupyterJup details 1Jup details 2

0 Answers
Related