The Easiest Way to Use Pandas in Python: import pandas as pd


pandas is an open source data analysis library built on top of the Python programming language.

The most common way to import pandas into your Python environment is to use the following syntax:

import pandas as pd

The import pandas portion of the code tells Python to bring the pandas data analysis library into your current environment.

The as pd portion of the code then tells Python to give pandas the alias of pd. This allows you to use pandas functions by simply typing pd.function_name rather than pandas.function_name.

Once you’ve imported pandas, you can then use the functions built in it to create and analyze data.

How to Create Series & DataFrames

The most common data types you’ll work with in pandas are series and DataFrames.

1. Series

A Series is a 1-dimensional array. The following code shows how to quickly create a Series using pandas:

import pandas as pd 

#define Series
x = pd.Series([25, 12, 15, 14, 19, 23, 25, 29])

#display Series
print(x)

0    25
1    12
2    15
3    14
4    19
5    23
6    25
7    29
dtype: int64

2. DataFrame

A DataFrame is a 2-dimensional array. The following code shows how to quickly create a DataFrame using pandas:

import pandas as pd 

#define DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#display DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12

Potential Errors when Importing Pandas

There are two potential errors you may encounter when import pandas:

1. NameError: name ‘pd’ is not defined

One error you may encounter is:

NameError: name 'pd' is not defined

This occurs when you fail to give pandas an alias when importing it. Read this tutorial to find out how to quickly fix this error.

2. No module named pandas

Another error you may encounter is:

no module name 'pandas'

This occurs when Python does not detect the pandas library in your current environment. Read this tutorial to find out how to fix this error.

Additional Resources

If you’re looking to learn more about pandas, check out the following resources:

Complete List of Statology Python Guides
Online pandas documentation page
Python for Data Analysis by Wes McKinney
Official pandas Twitter page

Leave a Reply

Your email address will not be published.