1

I'd like to join/combine two columns in a data frame.

Data

import pandas as pd
dat = pd.DataFrame({'A' : [1, 2, 3], 'B' : [4, 5, 6]})

Desired Output

14
25
36
Vedda
  • 7,066
  • 6
  • 42
  • 77
  • See the answer here: https://stackoverflow.com/questions/37425628/combine-multiple-columns-into-1-column-python-pandas – swiftg Jul 06 '18 at 22:20

1 Answers1

1

Using apply with join

dat.astype(str).apply(''.join,1)
Out[210]: 
0    14
1    25
2    36
dtype: object

Or (PS not always work)

dat.A*10+dat.B
Out[211]: 
0    14
1    25
2    36
dtype: int64
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thanks! How do you define the columns in the first solution? – Vedda Jul 06 '18 at 21:37
  • @Vedda using `dat.astype(str).apply(''.join,1).to_frame('Yourname')` or you need ? dat[['A','B']].astype(str).apply(''.join,1) – BENY Jul 06 '18 at 21:44