<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged nameerror</title>
<link>https://coderlegion.com/tag/nameerror</link>
<description>Powered by Question2Answer</description>
<item>
<title>NameError: name 'pd' is not defined in Python [Solved]</title>
<link>https://coderlegion.com/156/nameerror-name-pd-is-not-defined-in-python-solved</link>
<description>&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;If you are a newbie to Python and are working with &lt;strong&gt;Pandas&lt;/strong&gt; module, you might get this error &quot;&lt;strong&gt;NameError: name 'pd' is not defined&lt;/strong&gt;&quot;. This error occurs when you are trying to work with &lt;strong&gt;Pandas&lt;/strong&gt; without properly defining the alias as &lt;strong&gt;pd&lt;/strong&gt;. The error is pretty straightforward, but it might leave you puzzled. No worries, in this article we will see what this error means, why it occurs, and how to solve it so let's get started.&lt;/p&gt;
&lt;h2 id=&quot;why-did-this-error-happen-&quot;&gt;Why did this error happen?&lt;/h2&gt;
&lt;p&gt;This error usually occurs while working with the &lt;strong&gt;Pandas&lt;/strong&gt; library in Python. Consider the following code block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt; import pandas # Importing pandas
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you run the above code it will give you the below error.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=16506121202321903188&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;The error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt; suggests that the name &lt;strong&gt;pd&lt;/strong&gt; is not defined which you are using in the code &lt;strong&gt;&lt;code&gt; df = pd.DataFrame({...})&lt;/code&gt;&lt;/strong&gt;. The Python interpreter cannot figure out what exactly &lt;strong&gt;pd&lt;/strong&gt; means. Great, now you know why you are getting this error let's move further.&lt;/p&gt;
&lt;h2 id=&quot;what-is-meant-by-an-alias-&quot;&gt;What is meant by an alias?&lt;/h2&gt;
&lt;p&gt;In the context of programming, an alias refers to an alternative name. It is a common practice for programmers to use aliases instead of actual names. Consider the following code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import pandas # Importing pandas
  df = pandas.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above code we are importing &lt;strong&gt;pandas&lt;/strong&gt; and then using it to create a data frame &lt;strong&gt;&lt;code&gt; pandas.DataFrame({...})&lt;/code&gt;&lt;/strong&gt;. Now what if we don't want to use the name &lt;strong&gt;pandas&lt;/strong&gt; instead we want to use a short name that's where aliases come into the picture. Consider the code below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  import pandas as pd # Importing pandas
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now in the above code, we have defined an alias name &lt;strong&gt;pd&lt;/strong&gt; as &lt;strong&gt;&lt;code&gt;import pandas as pd&lt;/code&gt;&lt;/strong&gt;, and later we are using &lt;strong&gt;&lt;code&gt;pd.DataFrame({...})&lt;/code&gt;&lt;/strong&gt; instead of &lt;strong&gt;&lt;code&gt;pandas.DataFrame({...})&lt;/code&gt;&lt;/strong&gt;. Now you know what an alias is and why programmers prefer to use an alias instead of simple imports so let's see the causes of this error.&lt;/p&gt;
&lt;h2 id=&quot;what-are-the-causes-of-the-error-&quot;&gt;What are the causes of the error?&lt;/h2&gt;
&lt;h3 id=&quot;1-module-not-imported&quot;&gt;1. Module not imported&lt;/h3&gt;
&lt;p&gt;If you have not imported the module &lt;strong&gt;&lt;code&gt;import pandas as pd&lt;/code&gt;&lt;/strong&gt; then you are going to get this error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt;. Consider the code below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;df = pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above code, you are trying to create a data frame but you have not imported the pandas module so it will result in the following error.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=17718313030780948174&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;h3 id=&quot;2-alias-not-defined&quot;&gt;2. Alias not defined&lt;/h3&gt;
&lt;p&gt;One reason for getting this error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt; is the alias is not properly defined. Take a look at the following code block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import pandas # Importing pandas
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating data frame
  print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After running the above code you will get the following error.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=17745414560096606305&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;You are getting this error because you have not defined the alias &lt;strong&gt;pd&lt;/strong&gt; that you are using in the code.&lt;/p&gt;
&lt;h3 id=&quot;3-using-local-scope&quot;&gt;3. Using local scope&lt;/h3&gt;
&lt;p&gt;Another reason for getting this error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt; is you have imported pandas within a local scope that will restrict its use. Consider the following code block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def create(): # Function for creating a data frame
    import pandas as pd
    pd.DataFrame({'name': ['ali','uzair','asim'], 'marks': [10,20,30]})
   

create() # Calling the create function
pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By running the above code you will get the following error.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=8762676568671733460&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;In this case, you have imported pandas with a proper alias &lt;strong&gt;&lt;code&gt;import pandas as pd&lt;/code&gt;&lt;/strong&gt; but still you are getting this error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt; the reason is the &lt;strong&gt;local scope&lt;/strong&gt; you have imported pandas inside a function and you are trying to create a data frame outside that function &lt;strong&gt;&lt;code&gt;pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]})&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&quot;div-red&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Caution:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;When working with functions be cautious about importing Pandas within a local scope. Importing Pandas locally can restrict its availability&lt;/span&gt;
&lt;/div&gt;
&lt;h2 id=&quot;how-to-solve-the-error-&quot;&gt;How to solve the error?&lt;/h2&gt;
&lt;h3 id=&quot;1-solution-for-a-module-not-imported&quot;&gt;1. Solution for a module not imported&lt;/h3&gt;
&lt;p&gt;If you haven't imported the panda's module and you are trying to access it then you will get this error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt;. To solve it you just need to import the module. Consider the code below.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import pandas as pd # Importing the pandas module

df = pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After importing the module &lt;strong&gt;&lt;code&gt;import pandas as pd&lt;/code&gt;&lt;/strong&gt; the error will be removed. Below is the output of the above code.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=1601039669656270263&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;You can use &lt;a rel=&quot;nofollow&quot; href=&quot;https://coderlegion.com/164/python-try-except-blocks?show=164#q164&quot; target=&quot;_blank&quot;&gt;try except blocks&lt;/a&gt; for proper error handling.&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Import the module first before using it.&lt;/span&gt;
&lt;/div&gt;
&lt;h3 id=&quot;2-solution-for-alias-not-defined&quot;&gt;2. Solution for alias not defined&lt;/h3&gt;
&lt;p&gt;The error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt; clearly says that the name &lt;strong&gt;pd&lt;/strong&gt; is not defined which simply means you are using &lt;strong&gt;pd&lt;/strong&gt; as an alias but haven't defined it which can be simply resolved by defining &lt;strong&gt;pd&lt;/strong&gt; as an alias. Consider the following code.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;  import pandas as pd # Importing pandas and defining alias as pd
  df = pd.DataFrame({'Marks': [50, 20, 30],
              'Status': ['Pass', 'Fail', 'Pass']}) # Creating a data frame
  print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output of the above code.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=2912634916125159969&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Define the alias properly before using it.&lt;/span&gt;
&lt;/div&gt;
&lt;h3 id=&quot;3-solution-for-local-scope&quot;&gt;3. Solution for local scope&lt;/h3&gt;
&lt;p&gt;In the case of local scope try importing pandas as global by doing that you can access it anywhere in your code.  Consider the following code block.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import pandas as pd # Importing pandas in global scope

def create(): # Function for creating a data frame
    pd.DataFrame({'name': ['ali','uzair','asim'], 'marks': [10,20,30]})
   
create() # Calling the create function
df = pd.DataFrame({'name': ['ali', 'uzair', 'asim'], 'cgpa': [3.0, 3.5, 3.9]}) # Creating a new data frame
print(df)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Output of the above code.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=15578182080672116880&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-green&quot;&gt;
&lt;span class=&quot;alert-header&quot;&gt;Tip:&lt;/span&gt;
&lt;span class=&quot;alert-body&quot;&gt;Import the module in the global scope to access it anywhere in your code.&lt;/span&gt;
&lt;/div&gt;
&lt;h3 id=&quot;4-solving-the-error-in-jupyter-notebook-or-google-colab&quot;&gt;4. Solving the error in Jupyter Notebook or Google Colab&lt;/h3&gt;
&lt;p&gt;Sometimes you might be working in Jupyter Notebook or Google Colab in this case if you are getting this error &lt;strong&gt;&quot;NameError: name 'pd' is not defined&quot;&lt;/strong&gt; check whether the alias &lt;strong&gt;pd&lt;/strong&gt; is defined in global scope and if not define it and rerun your code.&lt;br&gt;
If the alias is defined properly and you are still getting this error then it might be the following case.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=17692736680121642409&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;p&gt;where the import statement is in a separate cell and you are creating the data frame in a separate cell. In this case, run the previous cell first where you have imported &lt;strong&gt;Pandas&lt;/strong&gt;, and then the cell where you are creating the data frame.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;https://logiclair.org/?qa=blob&amp;amp;qa_blobid=105449581792552700&quot; alt=&quot;&quot;&gt;&lt;/p&gt;
&lt;div class=&quot;div-faq&quot;&gt;
&lt;span class=&quot;faq-header&quot;&gt;FAQ&lt;/span&gt;
&lt;span class=&quot;faq-body&quot;&gt;

Q. What does the error message &quot;NameError: name 'pd' is not defined&quot; mean?&lt;br&gt;  
A. The error suggests that you are using an alias name 'pd' which is not properly defined in your code. 
&lt;br&gt;&lt;br&gt;
Q. How can I resolve the &quot;NameError: name 'pd' is not defined&quot; error?&lt;br&gt; 
A. To resolve it try importing 'import pandas as pd' in a global scope.&lt;br&gt; 
  
&lt;/span&gt;
&lt;/div&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;In summary, the error indicates that you are using an alias &lt;strong&gt;pd&lt;/strong&gt; which is not defined in your code. To resolve this problem try to define the alias &lt;strong&gt;&lt;code&gt;import pandas as pd&lt;/code&gt;&lt;/strong&gt; in global scope first before using it. If you are working in Jupyter Notebook or Google Colab you have to define the alias as previous and also try to rerun the cell where you have imported pandas.&lt;/p&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://pandas.pydata.org/docs/&quot;&gt;https://pandas.pydata.org/docs/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.w3schools.com/PYTHON/python_scope.asp&quot;&gt;https://www.w3schools.com/PYTHON/python_scope.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://towardsdatascience.com/top-python-libraries-numpy-pandas-8299b567d955&quot;&gt;https://towardsdatascience.com/top-python-libraries-numpy-pandas-8299b567d955&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
<category>Python</category>
<guid isPermaLink="true">https://coderlegion.com/156/nameerror-name-pd-is-not-defined-in-python-solved</guid>
<pubDate>Thu, 29 Feb 2024 09:11:50 +0000</pubDate>
</item>
</channel>
</rss>