Home » How to Fix: module ‘pandas’ has no attribute ‘dataframe’

How to Fix: module ‘pandas’ has no attribute ‘dataframe’

by Erma Khan

One error you may encounter when using pandas is:

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

This error usually occurs for one of three reasons:

1. You write pd.dataframe instead of pd.DataFrame

2. Some other variable is named ‘pd’ or ‘pandas’

3. The file name is pd.py or pandas.py

The following examples show how to resolve this error in each of these scenarios.

Reason 1: Using pd.dataframe

Suppose we attempt to create a pandas DataFrame using the following syntax:

import pandas as pd

#attempt to create DataFrame
df = pd.dataframe({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

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

We receive an error because we wrote the word dataframe in lowercase.

To create a pandas DataFrame, we must write the word ‘DataFrame’ in camel-case:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

#view DataFrame
df

	points	assists
0	25	5
1	12	7
2	15	13
3	14	12

Notice that we’re able to successfully create the DataFrame without any errors.

Reason 2: Some other variable is named ‘pd’ or ‘pandas’

We might also receive this error if some other variable in our script is named ‘pd’ or ‘pandas’:

import pandas as pd

#create a list named 'pd'
pd = [1, 2, 3, 4]

#attempt to create DataFrame
df = pd.dataframe({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

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

To resolve this error, we simply need to rename the variable currently named ‘pd’ to something else:

import pandas as pd

#create a list named 'data'
data = [1, 2, 3, 4]

#create DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14],
                   'assists': [5, 7, 13, 12]})

#view DataFrame
df

	points	assists
0	25	5
1	12	7
2	15	13
3	14	12

Notice that we don’t receive an error because we no longer have a variable named py or pandas.

Reason 3. The file name is pd.py or pandas.py

Another reason you may receive an error is if the file name of your script is pd.py or pandas.py.

To resolve this error, you simply need to rename your file to something else like my_script.py or my_data.py or literally any other name.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes

Related Posts