This tutorial shows how to fix the error of has no attribute ‘dataframe’
in Python. The error could appear as follows.
AttributeError: 'int' object has no attribute 'DataFrame' AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'? AttributeError: partially initialized module 'pandas' has no attribute 'DataFrame' (most likely due to a circular import)
It occurs may be due to one of the following reasons.
- 1. There is another variable named as ‘pd’.
- 2. Wrote it as
pd.dataframe
, but the correct way ispd.DataFrame
. - 3. Save the Python file as pd.py or pandas.py.
Example 1: Another variable named as ‘pd’
The following Python code reproduces the error.
import pandas as pd
# the following variable is named as pd
pd=2
data = {'Brand': ['Tesla', 'Ford','Toyota'], 'Count': [3, 4, 5]}
df = pd.DataFrame(data=data)
print(df)
The following is the output, which reproduces the error.
AttributeError: 'int' object has no attribute 'DataFrame'
To fix this error, you need to change pd
to another name. The following changes pd
to variable_1
.
import pandas as pd
# changing the variable name from pd to variable_1
variable_1=2
data = {'Brand': ['Tesla', 'Ford','Toyota'], 'Count': [3, 4, 5]}
df = pd.DataFrame(data=data)
print(df)
The following the output. As you can see, it fixed the problem.
Brand Count 0 Tesla 3 1 Ford 4 2 Toyota 5 [Finished in 671ms]
Example 2: Incorrectly writing of DataFrame
DataFrame
needs to put letters of D and F to be capitalized. If not, then there will be an error. The following reproduces such error.
import pandas as pd
data = {'Brand': ['Tesla', 'Ford','Toyota'], 'Count': [3, 4, 5]}
# the writing of "dataframe" is incorrect:
df = pd.dataframe(data=data)
print(df)
The following is the error.
AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?
To fix that, we need to correctly write DataFrame. The following is the Python code.
import pandas as pd
data = {'Brand': ['Tesla', 'Ford','Toyota'], 'Count': [3, 4, 5]}
# correctly writing "DataFrame"
df = pd.DataFrame(data=data)
print(df)
The following is the output:
Brand Count 0 Tesla 3 1 Ford 4 2 Toyota 5 [Finished in 696ms]
Example 3: Save file name as pandas.py or pd.py
If you save your file as pandas.py or pd.py, you will see the following error.
AttributeError: partially initialized module 'pandas' has no attribute 'DataFrame' (most likely due to a circular import)
The solution is to change it into other names, such as file_1.py or data_file.py. Such error will disappear.