Skip to content
Related Articles

Related Articles

How to Use cbind in Python?

Improve Article
Save Article
Like Article
  • Last Updated : 28 Nov, 2021

In this article, we will discuss cbind in Python. We have seen cbind() function in R Programming language to combine specified Vector, Matrix, or Data Frame by columns. But in Python, there is concat() function which is equivalent to cbind() function of R.

Create dataframes for demonstration:

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:

Example 1: Concat dataframe with equal indices

This will concatenate row-wise data based on the index. here the two dataframe indices are the same.

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:

Example 2: Concat dataframe with unequal indices

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:

In order to remove these NaN rows, we have to drop the index by using reset_index() method

Syntax: dataframe.reset_index(drop=True, inplace=True)

Example:

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:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!