I have two columns in my dataframe.
var1 var2
01 001
I would like to create a third column that joins them together:
var1 var2 var3
01 001 01001
Does anyone know how to do this? Thank you!
You can use simple concatenate by +
with casting by astype
:
df['var3'] = df.var1.astype(str) + df.var2.astype(str)
print df
var1 var2 var3
0 01 001 01001
If type
of both columns is string
casting is omited:
print type(df.loc[0,'var1'])
<type 'str'>
print type(df.loc[0,'var2'])
<type 'str'>
df['var3'] = df.var1 + df.var2
print df
var1 var2 var3
0 01 001 01001
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With