Concatenate
Learn how to apply the concatenate() method to combine data in pandas.
Concept of concatenate
The term concatenate is defined as the action of linking or stitching objects together in a chain. In the context of pandas
, we concatenate different pandas
objects together, e.g., a DataFrame with another DataFrame. The type of concatenation we can perform on the objects depends on the axis of the linkage, i.e., either row-wise or column-wise.
For this lesson, we’ll use a mock dataset of cars insured by a motor insurance company to demonstrate how concatenation works.
# Read data from CSV filedf = pd.read_csv('../usr/local/data/csv/insured_cars.csv')# Display 10 sample rowsprint(df.sample(10))
Row-wise concatenation
In row-wise concatenation, we’re linking two pandas
objects on top of one another (i.e., vertically).
Let’s say we have two DataFrames df_A
and df_B
, where df_A
comprises data on cars with a model year before 2005 (inclusive). On the other hand, df_B
comprises data on cars with a model year after 2006 (inclusive).
# Display heads of DataFrames A and Bprint(df_A.sort_values(by='car_model_year').tail())# Print line separator to better visualize different outputsprint('=' * 65)print(df_B.sort_values(by='car_model_year').head())
To create a complete dataset (from df_A
and df_B
) with all the car model years present, the ...
Get hands-on with 1400+ tech skills courses.