Table of Contents
The NameError “name ‘pd’ is not defined” occurs when the variable or function “pd” has not been defined or imported in the current program. To fix this error, the user must either define or import the “pd” variable or function from its source library. This can be done by using the appropriate import statement or by defining the variable or function within the program. Once the “pd” variable or function is properly defined or imported, the NameError should be resolved.
Fix: NameError name ‘pd’ is not defined
One common error you may encounter when using Python is:
NameError: name 'pd' is not definedThis error usually occurs when you import the python library , but fail to give it the alias of pd when importing it.
The following examples illustrate how this error occurs in practice and how you can quickly fix it.
Example 1: Use import pandas as pd
Suppose you import the pandas library using the following code:
import pandasIf you then attempt to create a pandas DataFrame, you’ll get the following error:
#create pandas 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]})
#attempt to print DataFrame
print(df)
Traceback (most recent call last):
1 import pandas
----> 2 df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
3 'assists': [5, 7, 7, 9, 12, 9, 9, 4],
4 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
5
NameError: name 'pd' is not defined
To fix this error, you need provide the alias of pd when importing pandas
import pandas as pd
#create pandas 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]})
#print 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
Example 2: Use import pandas
Suppose you import the pandas library using the following code:
import pandasIf you then attempt to create a pandas DataFrame, you’ll get the following error:
#create pandas 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]})
#attempt to print DataFrame
print(df)
Traceback (most recent call last):
1 import pandas
----> 2 df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
3 'assists': [5, 7, 7, 9, 12, 9, 9, 4],
4 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})
5
NameError: name 'pd' is not defined
To fix this error, you could simply choose not to use the alias of pd at all:
importpandas
#create pandas DataFrame
df = pandas.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]})
#print 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 12Note: The syntax “import pandas as pd” is commonly used because it offers a more concise way to use pandas functions. Instead of typing “pandas” each time, you can simply type in “pd” which is quicker and easier to read.
Cite this article
stats writer (2024). How can I fix the NameError “name ‘pd’ is not defined”?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-fix-the-nameerror-name-pd-is-not-defined/
stats writer. "How can I fix the NameError “name ‘pd’ is not defined”?." PSYCHOLOGICAL SCALES, 30 Apr. 2024, https://scales.arabpsychology.com/stats/how-can-i-fix-the-nameerror-name-pd-is-not-defined/.
stats writer. "How can I fix the NameError “name ‘pd’ is not defined”?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-fix-the-nameerror-name-pd-is-not-defined/.
stats writer (2024) 'How can I fix the NameError “name ‘pd’ is not defined”?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-fix-the-nameerror-name-pd-is-not-defined/.
[1] stats writer, "How can I fix the NameError “name ‘pd’ is not defined”?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, April, 2024.
stats writer. How can I fix the NameError “name ‘pd’ is not defined”?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
