Saara Ligamena
Saara Ligamena

Reputation: 45

Concatenation in Pandas Python

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.

Upvotes: 0

Views: 88

Answers (4)

Nayana Madhu
Nayana Madhu

Reputation: 1225

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)

Upvotes: 0

PraneetNigam
PraneetNigam

Reputation: 997

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

Upvotes: 1

DGav
DGav

Reputation: 281

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

Upvotes: 0

A.Kot
A.Kot

Reputation: 7903

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)

Upvotes: 0

Related Questions