By Kevin McAleer, 5 Minutes

Welcome to the exciting lesson on Data Frames in Pandas. Data Frames are one of the most important and widely used data structures in Pandas. They allow you to store and manipulate tabular data efficiently. In this lesson, we’ll explore creating Data Frames, performing basic operations, and how they can be used in data analysis.
A Data Frame in Pandas is a two-dimensional, size-mutable1, and potentially heterogeneous2 tabular data structure with labeled axes (rows and columns). It’s akin to a spreadsheet or SQL table and is the most commonly used Pandas object.
A Pandas Data Frame

Data Frame Axis
A Data Frame has two axes. Axis 0 runs down the rows, and its labels are called the index. Axis 1 runs across the columns, and its labels are the column names. You’ll see axis=0 and axis=1 in many Pandas methods.

Data Frame Series
Data Frame Series are one-dimensional labeled arrays capable of holding data of any type (integer, string, float, etc.). They are the building blocks of Data Frames.

Data Frame Rows
Data Frame Rows are the horizontal lines that contain the data. Each row is assigned a unique index value.

DataFrames, a fundamental feature of pandas in Python, are widely used in data analysis for several reasons:
Structured Data Representation: DataFrames provide a tabular structure, which is intuitive and aligns well with how data is often organized (similar to spreadsheets).
Efficient Data Manipulation: They allow for efficient, easy manipulation of data, including filtering, replacing, and aggregating values.
Handling Large Datasets: DataFrames are optimized for performance, enabling the handling of large datasets effectively.
Data Analysis: They offer numerous built-in methods for data analysis, making it easier to perform complex statistical analysis, groupings, and pivots.
Integration with Other Tools: DataFrames seamlessly integrate with a variety of data sources and can be easily exported to different file formats.
Visualization Support: They are compatible with various data visualization libraries, simplifying the creation of charts and graphs from the data.
Ease of Use: Pandas DataFrames have a user-friendly syntax, making data manipulation and analysis more accessible.
In summary, DataFrames simplify data manipulation and analysis, making them a preferred choice for data scientists and analysts.
You can create a Data Frame from various sources like lists, dictionaries, or external data sources (CSV, Excel files). Here’s an example of creating a Data Frame from a dictionary:
You can check the shape of a Data Frame using df.shape:
print(df.shape)
This will produce the output:
(3, 3)
This means the Data Frame has 3 rows and 3 columns.
To view the top and bottom rows of the frame, use df.head() and df.tail():
print(df.head()) # first five rows
print(df.tail()) # last five rows
Our Data Frame only has three rows, so both show all of it. Pass a number to see fewer, like df.head(2).
You can select a specific column or row from a Data Frame:
# Selecting a column
print(df['Name'])
# Selecting a row by position (rows count from 0, so this is Bob)
print(df.iloc[1])
You can easily add new columns or remove existing ones:
# Adding a new column
df['Salary'] = [70000, 80000, 90000]
# Deleting a column
del df['Age']
You can easily replace data in a column:
# Replacing data in a column
df['Salary'] = [75000, 85000, 95000]
If you want to replace one value wherever it appears, you can use replace():
# Replace every 75000 with 76000
df['Salary'] = df['Salary'].replace(75000, 76000)
To change one particular cell, use loc with the row label and column name:
df.loc[0, 'Salary'] = 77000 # Alice's salary
Experiment with what you have learned in this lesson below:
Quick quiz 1 of 2
A Data Frame has 10 rows and 4 columns. What does df.shape return?
shape is always (rows, columns).Quick quiz 2 of 2
Which axis runs across the columns?
This lesson introduced the basics of Data Frames in Pandas. We explored how to create Data Frames, perform basic operations, and how they serve as a cornerstone for data manipulation in Python.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments