As the number of fields is growing in each industry, in each Data sources. It is almost impossible to store all the variables in single Data table. So ideally we received Data tables in multiple files. In these situation, whenever there is a need to bring variables together in one table, merge or join is helpful. Cross join creates a table with cartesian product of observation between two tables. For each row of table 1, a mapping takes place with each row of table 2. The below article discusses how to Cross join Dataframes in Python.
Amy has two Dataframes, Customer Data 1 with 10 observation. This Data has Customer ID, First Name, Last Name and Gender. Customer ID is the primary key. Customer Data 2 has 12 observation. This Data has Customer ID as primary key, First Name, Last Name, Country Name and Total Spend in an year. Amy wants to create a table with all combination of observations between table 1 and table 2.
Below are the key steps to follow to right join Pandas Dataframe:
- Step 1: Import all the necessary modules.
import pandas as pd
- Step 2: Use merge function from Pandas module to merge dataframes. To do the right join, how=”cross” parameter helps.
Merged_Data=Customer_Data_1.merge(Customer_Data_2, how='cross')
- Step 3: Check the output data quality to assess the observations in final Dataframe. Please note that as the Customer Data 1 has 10 and Customer Data 2 has 12 observations, so the final Dataframe also has 120 observation. Product of Number of observation present in Table 1 and Number of observation present in table 2 is present in Cross Join output.
Merged_Data.head(10) #Printing shape of Dataframe, i.e. Number of Rows an Number of Columns Merged_Data.shape
Thus, Amy is able to create single Dataframe as per her requirement in Python.
To get top certifications in Python and build your resume visit here. Also, you can read books listed here to build strong knowledge around Python.
Visit us below for video tutorial:
Looking to practice more with this example? Drop us a note, we will email you the Code file:
📬 Stay Ahead in Data Science & AI – Subscribe to Newsletter!
- 🎯 Interview Series: Curated questions and answers for freshers and experienced candidates.
- 📊 Data Science for All: Simplified articles on key concepts, accessible to all levels.
- 🤖 Generative AI for All: Easy explanations on Generative AI trends transforming industries.
💡 Why Subscribe? Gain expert insights, stay ahead of trends, and prepare with confidence for your next interview.
👉 Subscribe here: