Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
DataFrames | Pandas
Unveiling the Power of Data Manipulation with Pandas
course content

Course Content

Unveiling the Power of Data Manipulation with Pandas

bookDataFrames

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:

1234567
import pandas as pd # Create a DataFrame from a dictionary df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Print the DataFrame print(df)
copy

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.

123456789
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]}) # Access the value at row 1, column 0 print(df.iloc[1, 0]) # Access the value at row 1, column 'B' print(df.loc[1, 'B'])
copy

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!

Task
test

Swipe to show code editor

  1. Import the pandas library with the pd alias.
  2. Create a dictionary named data with the list [1, 2, 3, 4, 5] as the value for the key A.
  3. Create a new DataFrame from the data dictionary and assign it to a variable named df.
  4. Print the data type of df.
  5. Print the df DataFrame.
  6. Access the element at row index 2 and column index 2

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt