DataFrame is an attribute of the pandas module. If you encounter this error, you have either misspelt DataFrame as dataframe, overridden the pandas import with a variable named pandas or pd, or you have a python script in your working directory called pandas.py or pd.py.

This tutorial will go through how to solve each scenario with code examples.


Example #1: Misspelling DataFrame

We can create a DataFrame object using DataFrame() method. However, if we use dataframe(), we will throw an AttributeError because dataframe is not an attribute in pandas. Let’s look at an example:

import pandas as pd

data = {"name":['Zeus', 'Hades', 'Achilles'],
"score":[10, 8, 9]}

df = pd.dataframe(data)

print(df)

Let’s run the code to see what happens.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [1], in <cell line: 6>()
      1 import pandas as pd
      3 data = {"name":['Zeus', 'Hades', 'Achilles'],
      4 "score":[10, 8, 9]}
----> 6 df = pd.dataframe(data)
      8 print(df)

File ~/opt/anaconda3/lib/python3.8/site-packages/pandas/__init__.py:261, in __getattr__(name)
    257     from pandas.core.arrays.sparse import SparseArray as _SparseArray
    259     return _SparseArray
--> 261 raise AttributeError(f"module 'pandas' has no attribute '{name}'")

AttributeError: module 'pandas' has no attribute 'dataframe'

Solution

To solve this error, we need to spell dataframe correctly as DataFrame. Let’s look at the revised code:

import pandas as pd

data = {"name":['Zeus', 'Hades', 'Achilles'],
"score":[10, 8, 9]}

df = pd.DataFrame(data)

print(df)

Let’s run the code to see the result:

   name  score
0      Zeus     10
1     Hades      8
2  Achilles      9

Example #2: Overriding Pandas Import with Variable Name

We can also throw this error if another variable in our script is called pd or pandas. Let’s look at an example:

import pandas as pd

pd = [3, 2, 4]

data = {"name":['Zeus', 'Hades', 'Achilles'],
"score":[10, 8, 9]}

df = pd.DataFrame(data)

print(df)

In the above code, we import the pandas library as pd. Then we define a list called pd, which overrides the library import. When we try to create a DataFrame by calling the DataFrame() method, we call the method on a list. Let’s run the code to see what happens:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [3], in <cell line: 8>()
      3 pd = [3, 2, 4]
      5 data = {"name":['Zeus', 'Hades', 'Achilles'],
      6 "score":[10, 8, 9]}
----> 8 df = pd.DataFrame(data)
     10 print(df)

AttributeError: 'list' object has no attribute 'DataFrame'

Solution

We need to rename the variable pd to something more representative to solve this error. Because the list contains the old scores for game players, we will rename the list old_scores. The rest of the code will remain the same.

import pandas as pd

old_scores = [3, 2, 4]

data = {"name":['Zeus', 'Hades', 'Achilles'],
"score":[10, 8, 9]}

df = pd.DataFrame(data)

print(df)

Let’s run the code to see the result:

    name  score
0      Zeus     10
1     Hades      8
2  Achilles      9

Example #3: File Named pandas.py or pd.py

We may also encounter this error if a Python script is called pd.py or pandas.py in our working directory. Let’s look at an example where we have a script called pandas.py that prints Hello world!.

print(Hello world!')

We will save the code from the above example in a separate file called scores.py.

import pandas as pd

data = {"name":['Zeus', 'Hades', 'Achilles'],
"score":[10, 8, 9]}

df = pd.DataFrame(data)

print(df)

If we list the files in our working directory using ls, we will see two files:

pandas.py   scores.py

Next, we will try to run the scores.py script to create a DataFrame.

python scores.py
Hello world!
Traceback (most recent call last):
  File "scores.py", line 6, in <module>
    df = pd.DataFrame(data)
AttributeError: module 'pandas' has no attribute 'DataFrame'

We get the AttributeError because the scores.py has an import statement.

When we import the module pandas, the interpreter first searches for a built-in module with that name. If not found, the interpreter then searches for a file called pandas.py in the directory containing the input script, then the PYTHONPATH, and then the installation-dependent default.

Because pandas is not a built-in module, the interpreter looks for and finds the file called pandas.py in our working directory and imports it, which is why we see Hello world! printed to the console. But our pandas.py script does not have DataFrame as an attribute.

Solution

To solve this error, we need to rename the pandas.py script so that it does not share the same name as the pandas library. We will rename the script hello_world.py.

mv pandas.py hello_world.py
python scores.py

Let’s run scores.py again to get the result:

       name  score
0      Zeus     10
1     Hades      8
2  Achilles      9

Summary

Congratulations on reading to the end of this tutorial! The AttributeError: module ‘pandas’ has no attribute ‘DataFrame’ occurs when you misspell DataFrame or override the pandas import. You can solve this error by ensuring your variables and python scripts are not named after pandas. Generally, do not name variables or scripts after existing Python libraries.

For further reading on Pandas, go to the article: Introduction to Pandas: A Complete Tutorial for Beginners.

Have fun and happy researching!