ModuleNotFoundError: No module named ‘pandas’ in Python

Python supports multiple standards and open source modules/libraries that are used to perform a variety of tasks. Python “pandas” library is used in programs to analyze, organize, manipulate data, etc., using its various functions. All these functions are only accessed when this library is installed in Python. Otherwise, the “no module named pandas” error appears in the Python program.

To Resolve this error, this Python blog will provide you the solutions for the “pandas module not installed error” in Python using the following contents:

Reason: pandas Module Not Installed in Python

The prominent reason which causes this “ModuleNotFoundError: No module named pandas” is when the user tries to import the “pandas” library without installing it in Python. Let’s look at the following code, which invokes this “ModuleNotFoundError” in the program:

The above output snippet shows the error “No module named pandas” because the library is accessed without being installed in Python.

Solution 1: Install the pandas Module in Python (For Windows)

To resolve this error, we need to install the “pandas” library in Python. The following steps will be followed to install the “pandas” module in Python:

Step 1: Open Command Prompt Terminal

Open the “cmd” from the start menu just like as given below:

Step 2: Install pandas Module in cmd

Now after opening the “cmd”, type the following given below command to install the “pandas” module in Python:

> pip install pandas

From the above snippet, it is verified that the “pandas” library is successfully installed in Python.

If you get a permission error while installing the “pandas” library from the above pip command, then type the following command in cmd:

> pip install pandas --user

Note:

To install the “pandas” library in Anaconda and Jupyter Notebook, type the following command respectively:

# for Anaconda
conda install -c anaconda pandas

# for Jupyter Notebook
!pip install pandas

Step 3: Verification of pandas Library

To verify the installation of pandas library, type the following command to check the details regarding version and file location:

> pip show pandas

The above snippet shows the name, version, file location, etc. of the installed “pandas” library.

Solution 2: Install the pandas Module in Python (For Linux)

To install the “pandas” module in Python Linux, you can use the “pip”  package manager. Type the given below command in the terminal to install Python “pandas” module:

$ pip3 install pandas

The “pip” method is recommended for installation of the “pandas” library because sometimes Linux distributions do not find all versions of pandas.

Example: Creating DataFrame Using pd.DataFrame() Function

In the example given below, the “pd.dataframe()” function is used to create the data frame by accepting the dictionary value as an argument. The example code is demonstrated below with its working:

Code:

import pandas as pd

dict_value = {'Name': ['Alex','Lily'], 'Marks': [55, 70]}
data_frame= pd.DataFrame(dict_value)
print(data_frame)

In the above code, the “pandas” module is imported as “pd” and the function “pd.DataFrame()” is used to return the dictionary as a data frame.

Output:

The “pandas” DataFrame has been successfully created in the above output using the “DataFrame()” function.

Conclusion

The “ModuleNotFoundError: No module named pandas” occurs when the user tries to access the “pandas” module without installing it in Python. The error is resolved when the “pandas” module is installed in Python using the command, i.e., “pip install pandas”. To resolve the “no module named pandas” in Linux, various commands are utilized in the terminal, such as pip, apt, etc. This article explained the reason and solutions of “No module named pandas” in Python.