Reputation: 175
In my dataframe I have 2 columns:
I want to concatenate them and it's easy:
sk_df['id'] = sk_df['country index'].str.cat(sk_df['id_number'].values.astype(str))
But some of rows in column id_number
has number of digits less than 8. In this case I want to add zeros as separator between Country index and id_number
(for example, if length of id_number
is 6 I want to add 8-6 = 2 zeros between variables: SK00813841
. If id_number
length is 7,than add 1 zero etc)
I tried this:
def indexing(row):
if row['id_number'].astype(str).str.len() == 8:
return row['country index'].str.cat(row['id_number'].values.astype(str))
else:
sep_mult = 8 - row['id_number'].astype(str).str.len()
return row['country index'].str.cat(row['id_number'].values.astype(str),sep = '0'*sep_mult)
sk_df['id'] = sk_df.apply(lambda row: indexing(row),axis = 1)
But it doesn't work. How can I do it?
Upvotes: 2
Views: 10359
Reputation: 3130
Use .zfill()
:
sk_df['id'] = sk_df['country index'] + sk_df['id_number'].astype(str).str.zfill(8)
Upvotes: 6