How to Use cbind in Python?


Python is a versatile programming language that offers programmers various modules and libraries to perform the required tasks. One such powerful function that Python offers is the “cbind”. This stands for column bind. The “cbind” is a powerful tool that allows programmers to combine, merge, and group the arrays, data frames, etc., in Python column-wise. In this article, we will learn how to use the “cbind” in Python.

Using zip and list comprehension

Zip and list comprehension are two very popular techniques used in many expressions in Python. The zip function can help to combine multiple elements from different iterables. On the other hand, list comprehension is a technique to generate elements for a list by combining multiple expressions, loops, etc., in a single line.

Syntax

zip(iterable1, iterable2, other iterables……….)

The zip function takes multiple iterable elements. Here iterable1, iterable2, iterable3, etc. are all the iterables like lists, etc. zip method will return a tuple containing all the elements combined. The iterables do not need to be in the same dimension.Also the iterables can be of multiple data types

Example 

In the following example, we have created three columns, namely column 1, column 2, and column 3. Next, we used the list comprehension and zip method to generate a list. We combined all three lists using the zip method and appended the elements to the list

column1 = [1, 2, 3]
column2 = [4, 5, 6]
column3 = [7, 8, 9]
combined = [list(t) for t in zip(column1, column2, column3)]
for row in combined:
    print(row)

Output

[1, 4, 7]
[2, 5, 8]
[3, 6, 9]

Using numpy.concatenate() Method

The concatenate, as the name suggests, helps to concatenate arrays along the specific axis (either row or column-wise). After concatenating the arrays, we can slice the desired elements from the result

Example

In the following code, we first imported the Numpy library. We created three arrays named column 1, column 2, and column 3. We used the concatenate method of Numpy to concatenate the arrays and stored the result in a variable named combined. Next, we iterated through the variable combined and printed the rows.

import numpy as np
column1 = np.array([1, 2, 3])
column2 = np.array([4, 5, 6])
column3 = np.array([7, 8, 9])
combined = np.concatenate((column1[:, np.newaxis], column2[:, np.newaxis], column3[:, np.newaxis]), axis=1)
for row in combined:
    print(row)

Output

[1 4 7]
[2 5 8]
[3 6 9]

Use zip and * Operator

zip method, as discussed earlier, helps to combine multiple iterable elements. On the other hand, the "*" operator is the unpacking operator that helps unpack iterable elements into separate values or arguments. It can be used for many contexts, e.g., function calls, list creations, assignment of variables, etc.

Example

column1 = [1, 2, 3]
column2 = [4, 5, 6]
column3 = [7, 8, 9]
combined = [*zip(column1, column2, column3)]
for row in combined:
    print(row)

Output

(1, 4, 7)
(2, 5, 8)
(3, 6, 9)

Using cbind with NumPy

Numpy is a popular library of Python to deal with numeric computations. It provides a straightforward built-in method to perform the “cbind” operation

Syntax

result = np.c_[array1, array2, array3,......]

Here array1, array2, array3, and so on are the arrays we need to perform the “cbind” operation. We can use single or multiple arrays on NumPy with the c_ method. All the arrays should be of the same dimension. Otherwise, Numpy would throw an error.

Example

In the following example, we imported the Numpy array and used aliasing to give it the alias name np. Next, we created array1 and array2 using the array method of Numpy. Next, we performed the “cbind” operation on the two arrays and printed the result.

The code uses the c_ method to perform column-wise concatenation. Although “cbind” is not mentioned, the function does exactly what the “cbind” does in other programming languages like R.

import numpy as np
array1 = np.array([1, 2, 3])
array2 = np.array([4, 5, 6])
result = np.c_[array1, array2]
print(result)

Output

[[1 4]
 [2 5]
 [3 6]]

Using cbind with pandas

Pandas is a powerful data analysis tool in Python. Panda has a buIlt function named concat to perform the concatenation. We only need to pass one additional argument named axis to the function to perform the operation column-wise. This also serves the same purpose as the “cbind” in other programming languages like R.

Syntax

result = pd.concat([df1, df2, df3, ….. ], axis=<1 or 0>)

Example

import pandas as pd
df1 = pd.DataFrame({'A': [1, 2, 3]})
df2 = pd.DataFrame({'B': [4, 5, 6]})
result = pd.concat([df1, df2], axis=1)
print(result)

Output

   A  B
0  1  4
1  2  5
2  3  6

Conclusion

In this article, we understood how to perform the “cbind” operation in Python with the help of the functions available in the libraries. The Numpy has the c_ method, which allows column-wise concatenation. Similarly, the Pandas have the concat method to perform the concatenation, which we can use to perform the “cbind”.

Updated on: 28-Jul-2023

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements