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 as pd
data1 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject1' : [ 'python' , 'R' , 'php' ],
'marks' : [ 96 , 89 , 90 ]})
data2 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject2' : [ 'html' , '.net' , 'jsp' ],
'marks' : [ 89 , 79 , 80 ]})
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 as pd
data1 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject1' : [ 'python' , 'R' , 'php' ],
'marks' : [ 96 , 89 , 90 ]})
data2 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject2' : [ 'html' , '.net' , 'jsp' ],
'marks' : [ 89 , 79 , 80 ]})
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 as pd
data1 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject1' : [ 'python' , 'R' , 'php' ],
'marks' : [ 96 , 89 , 90 ]}, index = [ 0 , 1 , 2 ])
data2 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject2' : [ 'html' , '.net' , 'jsp' ],
'marks' : [ 89 , 79 , 80 ]}, index = [ 3 , 4 , 5 ])
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 as pd
data1 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject1' : [ 'python' , 'R' , 'php' ],
'marks' : [ 96 , 89 , 90 ]}, index = [ 0 , 1 , 2 ])
data2 = pd.DataFrame({ 'name' : [ 'sravan' , 'harsha' , 'jyothika' ],
'subject2' : [ 'html' , '.net' , 'jsp' ],
'marks' : [ 89 , 79 , 80 ]}, index = [ 3 , 4 , 5 ])
data1.reset_index(drop = True , inplace = True )
data2.reset_index(drop = True , inplace = True )
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
Share your thoughts in the comments
Please Login to comment...