How to Use cbind in Python?
Last Updated :
24 Jan, 2024
In Python, the equivalent function cbind
in R is typically achieved using the concat
function from the Pandas
library. In this article, we will discuss cbind in Python. We have seen cbind() function in R Programming language to combine specified Vector, Matrix, or DataFrame by columns. But in Python, there is concat() function which is equivalent to cbind() function of R.
Creating DataFrame for Demonstration
We will create a sample Pandas DataFrame that we will use in our article.
Python3
# import pandas module
import pandas as pd
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject1': ['python', 'R', 'php'],
'marks': [96, 89, 90]})
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject2': ['html', '.net', 'jsp'],
'marks': [89, 79, 80]})
# display
print(data1)
print(data2)
Output:
name subject1 marks
0 sravan python 96
1 harsha R 89
2 jyothika php 90
name subject2 marks
0 sravan html 89
1 harsha .net 79
2 jyothika jsp 80
Using cbind in Python
Below are some examples by which we will see how to cbind DataFrames Pandas Python and also combining DataFrames using cbind in Pandas Python:
Concat Dataframe with Equal Indices Using cbind
This will concatenate row-wise data based on the index. here the two dataframe indices are the same. In this example, two Pandas DataFrames, 'data1' and 'data2', containing information about students, subjects, and marks, are created. The `pd.concat()` function is then utilized with `axis=1` to perform a column-wise concatenation, effectively combining the information from both DataFrames into a single DataFrame based on the shared 'name' column.
Python3
# import pandas module
import pandas as pd
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject1': ['python', 'R', 'php'],
'marks': [96, 89, 90]})
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject2': ['html', '.net', 'jsp'],
'marks': [89, 79, 80]})
# concat dataframes
pd.concat([data1, data2], axis=1)
Output:
name subject1 marks name subject2 marks
0 sravan python 96 sravan html 89
1 harsha R 89 harsha .net 79
2 jyothika php 90 jyothika jsp 80
Concat DataFrame with Unequal Indices Using cbind
In this scenario, the two dataframes indices are unequal, when we apply concat() function, this will result into a new dataframe with NaN values.
Python3
# import pandas module
import pandas as pd
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject1': ['python', 'R', 'php'],
'marks': [96, 89, 90]}, index=[0, 1, 2])
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject2': ['html', '.net', 'jsp'],
'marks': [89, 79, 80]}, index=[3, 4, 5])
# concat dataframes
pd.concat([data1, data2], axis=1)
Output:
name subject1 marks name subject2 marks
0 sravan python 96 NaN NaN NaN
1 harsha R 89 NaN NaN NaN
2 jyothika php 90 NaN NaN NaN
3 sravan NaN NaN sravan html 89.0
4 harsha NaN NaN harsha .net 79.0
5 jyothika NaN NaN jyothika jsp 80.0
In order to remove these NaN rows, we have to drop the index by using reset_index() method.
Example:
In this example, two Pandas DataFrames, 'data1' and 'data2', are created with overlapping indices. To perform a column-wise concatenation using `pd.concat`, the indices of both DataFrames are reset using `reset_index(drop=True, inplace=True)` to avoid conflicts.
Python3
# import pandas module
import pandas as pd
# create dataframe
data1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject1': ['python', 'R', 'php'],
'marks': [96, 89, 90]}, index=[0, 1, 2])
# create dataframe
data2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],
'subject2': ['html', '.net', 'jsp'],
'marks': [89, 79, 80]}, index=[3, 4, 5])
# remove dataframe 1 indices
data1.reset_index(drop=True, inplace=True)
# remove dataframe 2 indices
data2.reset_index(drop=True, inplace=True)
# concat dataframes
pd.concat([data1, data2], axis=1)
Output:
name subject1 marks name subject2 marks
0 sravan python 96 sravan html 89
1 harsha R 89 harsha .net 79
2 jyothika php 90 jyothika jsp 80
Similar Reads
How to Call a C function in Python Have you ever came across the situation where you have to call C function using python? This article is going to help you on a very basic level and if you have not come across any situation like this, you enjoy knowing how it is possible.First, let's write one simple function using C and generate a
2 min read
bin() in Python Python bin() function returns the binary string of a given integer. bin() function is used to convert integer to binary string. In this article, we will learn more about Python bin() function. Example In this example, we are using the bin() function to convert integer to binary string. Python3 x = b
2 min read
B Tree in Python A B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. B Tree in Python Table of Content Characteristics of B-TreesTraversal of B-Tree in PythonSearch operation in B Tree in PythonInsert opera
14 min read
Using C codes in Python | Set 2 Prerequisite: Using C codes in Python | Set 1 In the previous article, we have discussed how to access C code in Python. Now, let's see how to access C functions. Code #1 : Accessing C functions with Python Python3 1== import work print ("GCD : ", work.gcd(35, 42)) print ("\ndivide :
2 min read
How to get value from address in Python ? In this article, we will discuss how to get the value from the address in Python. First, we have to calculate the memory address of the variable or python object which can be done by using the id() function. Syntax: id(python_object) where, python_object is any python variable or data structure like
4 min read
Using C codes in Python | Set 1 Prerequisite: How to Call a C function in Python Let's discuss the problem of accessing C code from Python. As it is very evident that many of Pythonâs built-in libraries are written in C. So, to access C is a very important part of making Python talk to existing libraries. There is an extensive C p
4 min read
How to Convert Bytes to Int in Python? Converting bytes to integers in Python involves interpreting a sequence of byte data as a numerical value. For example, if you have the byte sequence b'\x00\x01', it can be converted to the integer 1.Using int.from_bytes()int.from_bytes() method is used to convert a byte object into an integer. It a
3 min read
How To Read .Data Files In Python? Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary w
4 min read
Convert Bytes To Bits in Python Converting bytes to bits in Python involves representing each byte in its binary form, where each byte is composed of 8 bits. For example , a byte like 0xAB (which is 171 in decimal) would be represented as '10101011' in binary. Letâs explore a few techniques to convert bytes to bits in Python.Using
2 min read