Course Content
Unveiling the Power of Data Manipulation with Pandas
Unveiling the Power of Data Manipulation with Pandas
DataFrames
Let's start with the basics. What exactly is a DataFrame?
To recap, a pandas DataFrame is a two-dimensional, size-mutable, tabular data structure with labeled rows and columns. It is similar to a spreadsheet, an SQL table, or the data.frame
in R. A DataFrame consists of a collection of Series, each of which is a one-dimensional labeled array.
You can think of a DataFrame as a group of Series objects that share an index (the column names). For example:
The code above produces a pandas DataFrame with exactly three columns and three rows. Note that the first number for every row corresponds to the index. What if we need to access a cell in a specific position?
In pandas, loc()
and iloc()
are two methods to access rows and columns of a DataFrame. They are both attributes of the DataFrame object and allow you to access and manipulate the data in various ways.
The main difference between loc
and iloc
is that loc
uses label-based indexing, while iloc
uses integer-based indexing.
There are many other ways to create a DataFrame, such as from a list of dictionaries, from a NumPy array, or by loading data from a file.
We have already spent so many words on this. Let's practice these concepts!
Swipe to show code editor
- Import the
pandas
library with thepd
alias. - Create a dictionary named
data
with the list[1, 2, 3, 4, 5]
as the value for the keyA
. - Create a new
DataFrame
from thedata
dictionary and assign it to a variable nameddf
. - Print the data type of
df
. - Print the
df
DataFrame. - Access the element at row index 2 and column index 2
Thanks for your feedback!
Let's start with the basics. What exactly is a DataFrame?
To recap, a pandas DataFrame is a two-dimensional, size-mutable, tabular data structure with labeled rows and columns. It is similar to a spreadsheet, an SQL table, or the data.frame
in R. A DataFrame consists of a collection of Series, each of which is a one-dimensional labeled array.
You can think of a DataFrame as a group of Series objects that share an index (the column names). For example:
The code above produces a pandas DataFrame with exactly three columns and three rows. Note that the first number for every row corresponds to the index. What if we need to access a cell in a specific position?
In pandas, loc()
and iloc()
are two methods to access rows and columns of a DataFrame. They are both attributes of the DataFrame object and allow you to access and manipulate the data in various ways.
The main difference between loc
and iloc
is that loc
uses label-based indexing, while iloc
uses integer-based indexing.
There are many other ways to create a DataFrame, such as from a list of dictionaries, from a NumPy array, or by loading data from a file.
We have already spent so many words on this. Let's practice these concepts!
Swipe to show code editor
- Import the
pandas
library with thepd
alias. - Create a dictionary named
data
with the list[1, 2, 3, 4, 5]
as the value for the keyA
. - Create a new
DataFrame
from thedata
dictionary and assign it to a variable nameddf
. - Print the data type of
df
. - Print the
df
DataFrame. - Access the element at row index 2 and column index 2