Saturday, January 29, 2022

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

We get AttributeError: module ‘pandas’ has no attribute ‘dataframe’ when the Pandas module is unable to resolve and initialize the DataFrame class. The AttributeError usually occurs if the class is not defined in the module or if it has a different name.

In this article, let us take a look at what AttributeError: module ‘pandas’ has no attribute ‘dataframe’ error means and how to resolve this with examples.

AttributeError: module ‘pandas’ has no attribute ‘dataframe’ Solution

There are four main reasons behind this error. 

  1. Ignoring the case of while creating DataFrame
  2. Declaring the module name as a variable name
  3. Naming file as pd.py or pandas.py
  4. Pandas module is not installed

Reason 1 – Ignoring the case of while creating DataFrame

The DataFrame class name is case-sensitive and, it is represented in camel-case, if you are using pd.dataframe() all in lower case then you will get module ‘pandas’ has no attribute ‘dataframe’ as shown below.

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# create pandas DataFrame
df = pd.dataframe({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

print(df)

Output

raceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 7, in <module>
    df = pd.dataframe({'team': ['India', 'South Africa', 'New Zealand', 'England'],
    raise AttributeError(f"module 'pandas' has no attribute '{name}'")

AttributeError: module 'pandas' has no attribute 'dataframe'. Did you mean: 'DataFrame'?

We can fix the issue by changing the dataframe to DataFrame(lowercase to camel-case) to create the Pandas DataFrame. When we run the program now, we don’t get any errors and, the code runs successfully.

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

print(df)

Output

           team  points  runrate  wins
0         India      10      0.5     5
1  South Africa       8      1.4     4
2   New Zealand       3      2.0     2
3       England       5     -0.6     2

Reason 2 – Declaring the module name as a variable name

We get this error if you have named any variable as ‘pd‘ or ‘pandas‘ and try to create the DataFrame. 

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# variable named as pd
pd = [2, 3]
# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

print(df)

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\main.py", line 9, in <module>
    df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
AttributeError: 'str' object has no attribute 'DataFrame'

In our above code, we have a local variable named ‘pd‘ and Python will not be able to resolve the DataFrame as it is a local variable and not the Pandas module.

We can resolve this error by renaming the variable to something else.

# import pandas library
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# variable named as pd
data = [2, 3]
# create pandas DataFrame
df = pd.DataFrame({'team': ['India', 'South Africa', 'New Zealand', 'England'],
                   'points': [10, 8, 3, 5],
                   'runrate': [0.5, 1.4, 2, -0.6],
                   'wins': [5, 4, 2, 2]})

print(df)

Output

           team  points  runrate  wins
0         India      10      0.5     5
1  South Africa       8      1.4     4
2   New Zealand       3      2.0     2
3       England       5     -0.6     2

Reason 3 – Naming file as pd.py or pandas.py

If you have named the script as pd.py or pandas.py then you will get module ‘pandas’ has no attribute ‘dataframe’ error. 

This mainly happens because the file name will shadow the Pandas module and, it can mess up the module imports.

We can fix this issue by renaming the script to some other name such as “my_script.py”.

Reason 4- Pandas package is not installed

The last reason could be if Pandas library is not installed in the proper Python path. You can go and verify the Lib/site-packages path and see if the Pandas library exists. 

If it’s not present then you can install pandas by running the below command.

pip install pandas

Installation instructions for Anaconda is found here



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...