I would like to create a new column that will put together 2 columns together. I looked over the internet but found nothing. How could I do:
Ex:
A B
50.631456 5.57871
C
(50.631456, 5.57871)
I would like to create a new column that will put together 2 columns together. I looked over the internet but found nothing. How could I do:
Ex:
A B
50.631456 5.57871
C
(50.631456, 5.57871)
list
+ zip
is one efficient way:
df['C'] = list(zip(df.A, df.B))
# A B C
# 0 50.631456 5.57871 (50.631456, 5.57871)
Performance
As expected, df.apply
methods are loopy and inefficient for large dataframes, especially when combined with lambda
.
df = pd.concat([df]*10000)
%timeit list(zip(df.A, df.B)) # 3.14ms
%timeit df.apply(tuple, axis=1) # 378ms
%timeit df.apply(lambda x: (x.A,x.B), axis=1) # 577ms
Checkout DataFrame.apply
.
df = pd.DataFrame(np.random.randint(0, 10, (6, 2)), columns=['a', 'b'])
df['c'] = df.apply(tuple, axis=1)
df
returns
a b c
0 8 1 (8, 1)
1 3 3 (3, 3)
2 2 8 (2, 8)
3 6 2 (6, 2)
4 2 2 (2, 2)
5 8 5 (8, 5)
you can use apply.
df = pd.DataFrame({'A': {0: 50.631456}, 'B': {0: 5.57871}})
df
Out[162]:
A B
0 50.631456 5.57871
df['C'] = df.apply(lambda x: (x.A,x.B), axis=1)
df
Out[155]:
A B C
0 50.631456 5.57871 (50.631456, 5.57871)