Concatenate two numerical values to make a new column using pandas?
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
Convert to both columns to string and then join both the columns to form the third column.
Code:
df['var1']=df['var1'].astype(str)
df['var2']=df['var2'].astype(str)
df['var3'] = df[['var1', 'var2']].apply(lambda x: ''.join(x), axis=1)