Creating Data Frames And Concatenating Them In Python
In this post, we will learn how to create data frames in Python and merging them vertically and horizontally.
Creating Data Frames And Concatenating Them In Python
In this post, we will learn how to create data frames in Python and merging them vertically and horizontally.
CREATING A DATA FRAME
Creating a data frame includes
- Making a data list
- Converting the list into a data frame with specified column’s names
MAKING A DATA LIST
Let’s create a data frame named ‘df_A’.
import pandas as pd
list_A = [('Mary',18),('Josh', 25),('Ryan',30)]
CONVERTING A DATA FRAME WITH SPECIFIED COLUMNS’S NAMES
df_A = pd.DataFrame(list_A, columns= ['Name', 'Age'])
Output:
Now let’s create a second data frame named ‘df_B’.
B = [('April', 22), ('Eileen', 18),('George', 22)]
df_B = pd.DataFrame(B, columns= ['Name', 'Age'])
df_B
Output:
CONCATENATING (MERGING) TWO DATA FRAMES
There are two different ways the data frames can be merged depending on the specific purpose. Vertical concatenating
- VERTICAL CONCATENATING
vertical_concat = pd.concat([df_A,df_B])
Output:
The two data frames df_A and df_B are merged vertically and the resulting vertical_concat data frame now contains six records.
HORIZONTAL CONCATENATING
Now let’s try horizontal concatenating using these two data frames.
horizontal_concat = pd.concat([df_A, df_B], axis=1)
Notice the usage of the axis=1 in here for horizontal concatenating.
Output:
메타데이터
- post_id
- 871a22cc092e
- slug
- creating-a-data-frame-and-merging-them-871a22cc092e
- url
- https://medium.com/@ooemma83/creating-a-data-frame-and-merging-them-871a22cc092e
- canonical_url
- https://medium.com/@ooemma83/creating-a-data-frame-and-merging-them-871a22cc092e
- author_url
- https://medium.com/@ooemma83
- status
- ok
- fetched_at
- 2026-09-06 15:15:39