yef_dan_92
yef_dan_92

Reputation: 175

Concatenating string and integers in pandas dataframe(based on conditions)

In my dataframe I have 2 columns:

  1. Country index(For example SK)
  2. id_number(usually 8 digit,for example:98341852)

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

Answers (1)

Ken Wei
Ken Wei

Reputation: 3130

Use .zfill():

sk_df['id'] = sk_df['country index'] + sk_df['id_number'].astype(str).str.zfill(8)

Upvotes: 6

Related Questions