Import Pandas as pd in Python

To import pandas in Python with the standard alias pd, put this line at the top of your script or in a notebook cell:

import pandas as pd

Pandas must be installed in the Python environment running your code. The import itself produces no output when it succeeds; you can then use pandas to create tables, load files, and analyze data.

What Importing Pandas as pd Means

Installing pandas makes the package available to a Python environment; importing it makes the package available to your code under the name pd.

The statement has three parts:

  • import tells Python to load a module or package.
  • pandas identifies the package.
  • as pd assigns the shorter name pd in your code.

The alias is a convention, not a requirement or a separate package. It lets you write names such as pd.DataFrame and pd.read_csv instead of using the longer package name.

Pandas provides two main data structures: a one-dimensional Series and a two-dimensional DataFrame, according to Brown University’s pandas introduction. A DataFrame is a table with labeled rows and columns; a Series represents a labeled sequence, such as a column selected from that table.

Importing pandas does not load a dataset. You still need to create a DataFrame or read data from a file.

How to Install and Import Pandas in Python

Choose the installation location that matches where your Python code runs.

Where You Run Python Where to Install Pandas What Must Match
A script, terminal session, or code editor Your project’s terminal environment The Python interpreter used to install pandas and run the script
Jupyter Notebook or JupyterLab A notebook cell using the pip magic command The notebook’s active kernel

Step 1: Install Pandas in the Correct Environment

  1. Open the terminal or notebook associated with your project.
  2. Install pandas in the Python environment that runs your script or notebook. If pandas already imports successfully, you can skip installation.

For a Python script or terminal session, run this in your terminal, not at the Python >>> prompt:

python -m pip install pandas

Using python -m pip runs pip through the selected Python interpreter. This reduces the chance of installing pandas into a different Python installation from the one running your script.

The terminal examples here use the executable name python. If your system uses python3, use that executable consistently for installation and running scripts:

python3 -m pip install pandas

If your project uses a virtual environment, select or activate it before installing packages.

For Jupyter Notebook or JupyterLab, run this in a notebook cell:

%pip install pandas

The %pip magic runs pip within the current IPython kernel. It is preferable to a bare shell-based pip command when multiple Python environments are available.

Wait for installation to finish. If Jupyter requests a kernel restart, restart it before running your imports.

Step 2: Add the Import and a Small Test

  1. Open a new Python file named pandas_demo.py, or create a new notebook cell.
  2. Enter this complete example:
import pandas as pd
print("pandas version:", pd.__version__)
df = pd.DataFrame({
    "name": ["Ada", "Lin"],
    "score": [82, 95]
})
print(df)

The first line makes pandas available as pd. The remaining lines print the installed version and create a DataFrame from a dictionary.

Here, pd refers to the pandas package, while df refers to the particular table you created. Neither name is a built-in Python keyword.

Avoid naming the file pandas.py: that can make Python load your file instead of the installed package.

Step 3: Run the Code and Check the Result

For a saved script, open a terminal in the folder containing pandas_demo.py and run:

python pandas_demo.py

If you use the python3 executable, run:

python3 pandas_demo.py

In Jupyter, select the cell containing the example and press Shift+Enter.

  1. Confirm that the output shows the installed pandas version followed by a DataFrame with name and score columns:
  name  score
0  Ada     82
1  Lin     95

The version depends on your installation. The labels down the left side are the DataFrame’s automatically generated index.

If you see the table without an exception, pandas is installed, the import works, and the pd alias is available in that running session.

How to Fix Common Pandas Import Errors

Read the final line of the traceback before changing anything. A missing package, an undefined alias, and a conflicting filename require different fixes.

ModuleNotFoundError: No Module Named pandas

A ModuleNotFoundError for pandas means the Python interpreter running your code cannot find the package. The package may be missing entirely, or it may be installed in another environment.

First, identify the interpreter from the Python session, editor, or notebook where the failure occurs:

import sys
print(sys.executable)

For a script or editor, compare that path with the interpreter used by your terminal:

python -c "import sys; print(sys.executable)"

If the paths differ, select the intended interpreter in your editor or switch your terminal to the project’s environment. Then install pandas through that interpreter:

python -m pip install pandas

For a Jupyter notebook, install directly into the active kernel:

%pip install pandas

Restart the kernel if requested, then run your import and test code again. A successful terminal installation does not fix a notebook that is still using a different environment.

If installation fails, resolve the installation error first. Repeating the import cannot make a package available when installation did not complete.

NameError: Name pd Is Not Defined

A NameError for pd means the name pd does not exist in the current scope. It does not necessarily mean pandas is missing.

Common causes include:

  • The import line has not run.
  • A notebook kernel was restarted.
  • Notebook cells ran out of order.
  • Pandas was imported without the pd alias.
  • The import happened inside a function, but you are using pd outside it.

Run the alias import before the line that uses pd. In a notebook, run the import cell and then the dependent cells in order. In a script, keep the import near the top, outside functions when the rest of the module needs it.

Rerun the failing operation. If the import now raises ModuleNotFoundError, the immediate problem is the active environment rather than the alias.

AttributeError or a Partially Initialized pandas Module

Errors mentioning a “partially initialized module” or a missing attribute such as DataFrame can occur when a local file shadows the real package.

Check your project directory for:

  • A file named pandas.py.
  • A package directory named pandas.
  • Code that assigns a different object to pd.

Rename an unrelated local pandas.py file to something such as pandas_demo.py. Rename a conflicting local package directory if it is not intended to replace pandas.

Then restart the Python process or notebook kernel so it no longer holds the previously imported module in memory. Run import pandas as pd , then create and print a DataFrame to check that pandas works after the restart.

If you accidentally reassigned pd, rename that variable and rerun the import before using pandas. Reinstalling the package will not resolve a conflicting filename or variable assignment.

SyntaxError After Entering an Installation Command

A pip installation command belongs in a terminal, not directly in ordinary Python code. Entering it at the >>> prompt or placing it inside a .py file causes a syntax error.

Close the Python session or open a separate terminal, then run the terminal installation command there. In Jupyter, use the %pip form in a notebook cell.

After installation finishes, run the Python import in your script or Python session. Keep package installation commands out of application source files.

How to Confirm Pandas Is Ready

When pandas imports but you are unsure which installation you are using, inspect its version and location in the session running your code:

print(pd.__version__)
print(pd.__file__)

The first line identifies the installed version. The second shows the package file Python loaded. For a normal installed package, that location is typically inside the environment’s site-packages directory, not an unrelated pandas.py file in your project.

Before building on the example, confirm that:

  • Your editor or notebook uses the intended Python environment.
  • The imported package comes from the expected location.
  • Creating and printing a DataFrame works.
  • Your script includes its own import rather than depending on a previous interactive session.

Keep imports near the top of each script or notebook, avoid assigning other values to pd, and rerun import cells after restarting a kernel.

Once these checks pass, replace the sample table with your actual data. Pandas can read CSV data into a DataFrame using pd.read_csv, as covered in the University of Tennessee’s pandas guide.

Frequently Asked Questions

How Do I Import Pandas in IDLE Python?

You can import pandas directly in IDLE’s Shell or put the import in an editor file and choose Run > Run Module. For example, this imports pandas using its full name and prints the installed version:

import pandas
print(pandas.__version__)

An alias is optional; this example uses pandas rather than pd. If the import fails because the package is missing, pandas must be installed for the Python interpreter that launched IDLE.

How Do I Import Pandas in Notepad?

Notepad can edit a Python script, but it cannot execute the import. Write your pandas code in Notepad and save it as pandas_demo.py, choosing All Files in the save dialog so the filename does not become pandas_demo.py.txt.

Open a terminal in the file’s folder and run it with Python:

python pandas_demo.py

Python executes the file; pandas must be installed in the environment used by that command.

How Do I Import Pandas and Matplotlib in Python?

Import pandas with the alias pd and Matplotlib’s pyplot module with the alias plt. If either package is missing, install both in your project’s terminal environment:

python -m pip install pandas matplotlib

Then run this example in a Python script or notebook:

import pandas as pd
import matplotlib.pyplot as plt
df = pd.DataFrame({
    "day": ["Mon", "Tue", "Wed"],
    "sales": [12, 18, 15]
})
df.plot(x="day", y="sales", kind="bar", legend=False)
plt.ylabel("Sales")
plt.tight_layout()
plt.show()

On a local system with a graphical plotting backend, this displays a bar chart; a configured notebook can display it within the notebook.