I'm trying to generate a code for each row of a dataframe, such that, the first 2 characters are codes for a category, the next 3 characters are codes for a sub-category, and the last 3 numbers are iterative numbers for that sub-category, for example -
CTSCT001
CTSCT002
CTSCT003
My code looks like this -
for sub_cat in dict_subcat_codes.keys():
i=1
for index, row in df[['Sub Category'] == sub_cat].iterrows():
row['Code'] = dict_cat_codes[row['Category']]+dict_subcat_codes[row['Sub Category']]+f'{i:03}'
i+=1
When I debug the code by printing the generated codes, they seem to print fine, but they are not being assigned to the Code
column of the df
. Am I doing something wrong? Is their a better way to deal with this?