How to Use cbind in Python?

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.
Python3
# import pandas moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]})    # create dataframedata2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject2': ['html', '.net', 'jsp'],                      'marks': [89, 79, 80]})  # displayprint(data1)  print(data2)
Output:
This will concatenate row-wise data based on the index. here the two dataframe indices are the same.
Python3
# import pandas moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]})    # create dataframedata2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject2': ['html', '.net', 'jsp'],                      'marks': [89, 79, 80]})    # concat dataframespd.concat([data1, data2], axis=1)
Output:
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 moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]}, index=[0, 1, 2])    # create dataframedata2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject2': ['html', '.net', 'jsp'],                      'marks': [89, 79, 80]}, index=[3, 4, 5])    # concat dataframespd.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 moduleimport pandas as pd  # create dataframedata1 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject1': ['python', 'R', 'php'],                      'marks': [96, 89, 90]}, index=[0, 1, 2])    # create dataframedata2 = pd.DataFrame({'name': ['sravan', 'harsha', 'jyothika'],                      'subject2': ['html', '.net', 'jsp'],                      'marks': [89, 79, 80]}, index=[3, 4, 5])  # remove dataframe 1 indicesdata1.reset_index(drop=True, inplace=True)  # remove dataframe 2 indicesdata2.reset_index(drop=True, inplace=True)  # concat dataframespd.concat([data1, data2], axis=1)
Output:
Picked
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate through Excel rows in Python?
Rotate axis tick labels in Seaborn and Matplotlib
Deque in Python
Queue in Python
Defaultdict in Python
Check if element exists in list in Python
Python Classes and Objects
Bar Plot in Matplotlib
reduce() in Python
Python | Get unique values from a list