Pandas DataFrames

Pandas DataFrames

What is a DataFrame?

A Pandas DataFrame is a 2 dimensional data structure, like a 2 dimensional array, or a table with rows and columns.

Example

Create a simple Pandas DataFrame:

Result

Try it Yourself »


Locate Row

As you can see from the result above, the DataFrame is like a table with rows and columns.

Pandas use the loc attribute to return one or more specified row(s)

Example

Return row 0:

Result

Try it Yourself »

Note: This example returns a Pandas Series.

Example

Return row 0 and 1:

Result

Try it Yourself »

Note: When using [], the result is a Pandas DataFrame.


w3schoolsCERTIFIED.2022

Get Certified!

Complete the Pandas modules, do the exercises, take the exam, and you will become w3schools certified!

$10 ENROLL


Named Indexes

With the index argument, you can name your own indexes.

Example

Add a list of names to give each row a name:

Result

Try it Yourself »

Locate Named Indexes

Use the named index in the loc attribute to return the specified row(s).

Example

Return “day2”:

Result

Try it Yourself »


Load Files Into a DataFrame

If your data sets are stored in a file, Pandas can load them into a DataFrame.

Example

Load a comma separated file (CSV file) into a DataFrame:

Try it Yourself »

You will learn more about importing files in the next chapters.


Test Yourself With Exercises

Exercise:

Insert the correct Pandas method to create a DataFrame.

Start the Exercise

ArmenianEnglish