0

I am trying to concatenate these three columns, but the code i am using is giving me this output, i changed the format of all the columns to string:

Income_Status_Number   Income_Stability_Number  Product_Takeup_Number  Permutation
      1                      1                          2                1.012
      2                      1                          3                2.013
      1                      1                          1               1.011

this is the code i used:

df['Permutation']=df['Income_Status_Number'].astype(str)+""+df['Income_Stability_Number'].astype(str)+""+df['Product_Takeup_Number'].astype(str)

But I want my output to look like this:

Income_Status_Number   Income_Stability_Number  Product_Takeup_Number  Permutation
  1                      1                          2                     112
  2                      1                          3                     213
  1                      1                          1                     111

Please help.

2
  • Better is df['Permutation']=df['Income_Status_Number'].astype(str)+df['Income_Stability_Number'].astype(str)+df['Product_Takeup_Number'].astype(str), but it should working nice. Commented Jan 22, 2018 at 14:49
  • What's the purpose of adding "" between each concatenation? Commented Jan 22, 2018 at 14:53

4 Answers 4

1

The issue is that the first column is being treated as a float instead of an int. The simple way to solve this problem is to sum the values with multipliers to put the numbers is the correct space and let pandas realize that the number is an int:

df['Permutation'] = df['Income_Status_Number']*100 + df['Income_Stability_Number']*10 + df['Product_Takeup_Number']

Another solution is to use astype(int).astype to convert the number first, but that solution is somewhat slower:

10000 Runs Each

as_type
  Total: 9.7106s
  Avg: 971059.8162ns

Maths
  Total: 7.0491s
  Avg: 704909.3242ns
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like your first column is being read as a float right before you convert it to a string.

df['Permutation']=df['Income_Status_Number'].astype(int).astype(str)+df['Income_Stability_Number'].astype(int).astype(str)+df['Product_Takeup_Number'].astype(int).astype(str)

Comments

0

Try the following code to add a 'Permutation' column to your data frame formatted in the way you wanted:

df['Permutation'] = df[df.columns[0:]].apply(lambda x: ''.join(x.dropna().astype(int).astype(str)),axis=1)

Which give you the following dataframe:

df
  Income_Status_Number Income_Stability_Number Product_Takeup_Number  \
0                    1                       1                     2
1                    2                       1                     3
2                    1                       1                     1

  Permutation
0         112
1         213
2         111

Comments

0

I hope this one will work for you.

df['Permutation'] = df[df.columns].apply(lambda x: ' '.join(x.dropna().astype(int).astype(str)),axis=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.