Pandas – Merge two dataframes with different columns
Pandas support three kinds of data structures. They are Series, Data Frame, and Panel. A Data frame is a two-dimensional data structure, Here data is stored in a tabular format which is in rows and columns. We can create a data frame in many ways.
Here we are creating a data frame using a list data structure in python.
Python3
# import required moduleimport pandas # assign datal=["vignan","it","sravan","subbarao"] # create data framedf = pandas.DataFrame(l) # display dataframedf |
Output:
Here in the above example, we created a data frame. Let’s merge the two data frames with different columns. It is possible to join the different columns is using concat() method.
Syntax: pandas.concat(objs: Union[Iterable[‘DataFrame’], Mapping[Label, ‘DataFrame’]], axis=’0′, join: str = “‘outer'”)
- DataFrame: It is dataframe name.
- Mapping: It refers to map the index and dataframe columns
- axis: 0 refers to the row axis and1 refers the column axis.
- join: Type of join.
Note: If the data frame column is matched. Then empty values are replaced by NaN values.
Steps by step Approach:
- Open jupyter notebook
- Import necessary modules
- Create a data frame
- Perform operations
- Analyze the results.
Below are some examples based on the above approach:
Example 1
In this example, we are going to concatenate the marks of students based on colleges.
Python3
# importing pandas moduleimport pandas as pd # dictionary with list object in # values ie college detailsdetails = { 'Name': ['Sravan', 'Sai', 'Mohan', 'Ishitha'], 'College': ['Vignan', 'Vignan', 'Vignan', 'Vignan'], 'Physics': [99, 76, 71, 93], 'Chemistry': [97, 67, 65, 89], 'Data Science': [93, 65, 65, 85]} # converting to dataframe using DataFrame()df = pd.DataFrame(details) # print data framedf |
Output:
Python3
# creating another datadetails1 = { 'Name': ['Harsha', 'Saiteja', 'abhilash', 'harini'], 'College': ['vvit', 'vvit', 'vvit', 'vvit'], 'Physics': [69, 76, 51, 43], 'Chemistry': [67, 67, 55, 89], 'Maths': [73, 65, 61, 85]} # create dataframedf1 = pd.DataFrame(details1) # display dataframedf1 |
Output:
Python3
# concat dataframespd.concat([df, df1], axis=0, ignore_index=True) |
Python3
# concat when axis = 1pd.concat([df, df1], axis=1, ignore_index=True) |
Example 2:
Storing marks and subject details
Python3
# Import pandas libraryimport pandas as pd # initialize list of listsdata = [['sravan', 98.00], ['jyothika', 90.00], ['vijay', 79.34]] # Create the pandas DataFramedf = pd.DataFrame(data, columns=['Name', 'Marks']) # print dataframe.df |
Output:
Python3
# initialize list of listsdata1 = [['Haseen', 88.00, 5], ['ramya', 54.00, 5], ['haritha', 56.34, 4]] # Create the pandas DataFramedf1 = pd.DataFrame( data1, columns=['Name', 'Marks', 'Total subjects registered']) # print dataframe.df1 |
Output:
Python3
# concatenating data framepd.concat([df, df1], axis=0, ignore_index=True) |
Output:





Please Login to comment...