35
import pandas as pd

df = pd.read_csv('FBI-CRIME11.csv')

print(df.head())

Running this simple code gives me the error:

Traceback (most recent call last):
  File "C:/Users/Dita/Desktop/python/lessons/python.data/csv.py", line 1, in <module>
    import pandas as pd
  File "C:\python\lib\site-packages\pandas-0.19.1-py3.5-win-amd64.egg\pandas\__init__.py", line 37, in <module>
    import pandas.core.config_init
  File "C:\python\lib\site-packages\pandas-0.19.1-py3.5-win-amd64.egg\pandas\core\config_init.py", line 18, in <module>
    from pandas.formats.format import detect_console_encoding
  File "C:\python\lib\site-packages\pandas-0.19.1-py3.5-win-amd64.egg\pandas\formats\format.py", line 33, in <module>
    from pandas.io.common import _get_handle, UnicodeWriter, _expand_user
  File "C:\python\lib\site-packages\pandas-0.19.1-py3.5-win-amd64.egg\pandas\io\common.py", line 5, in <module>
    import csv
  File "C:\Users\Dita\Desktop\python\lessons\python.data\csv.py", line 4, in <module>
    df = pd.read_csv('FBI-CRIME11.csv')
AttributeError: module 'pandas' has no attribute 'read_csv'
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
Willstarr
  • 377
  • 1
  • 3
  • 12

5 Answers5

76

Try renaming your csv.py to something else, like csv_test.py. Looks like pandas is being confused about what to import.

AKX
  • 152,115
  • 15
  • 115
  • 172
15

Make sure you don't have a file called pandas.py in the directory you are executing your python file in.

More info in Euguene's answer in this post.

tommmm
  • 182
  • 1
  • 4
5

I had checked for the presence of csv.py and made sure there was no file of this name. I also tried pip uninstall pandas and then pip install pandas. I still got the same error.

What worked for me was: pip-autoremove.

First install it using pip install pip-autoremove.
Then, remove pandas using pip-autoremove pandas -y.
Next, reinstall it using pip install pandas.

The reason why this is necessary is that sometimes, when using uninstall, the package folder may still be present.

Atul Balaji
  • 804
  • 4
  • 15
  • 29
  • I tried this approach. but I end up getting this : ```pkg_resources.DistributionNotFound: The 'panadas' distribution was not found and is required by the application``` – julian joseph Oct 16 '20 at 11:37
3

I'm using Jupiter notebook and in my case I have imported and used pandas as below.

import pandas as pd
df = pd.read_csv('canada_per_capita_income.csv')

but it keeps throwing this error

DataFrame' object has no attribute 'read_csv

then i restart the kernel and rename the csv file to

income.csv

it solve my issue. Hope this might help someone


Nipun Ravisara
  • 3,629
  • 3
  • 20
  • 35
3

You need to make sure there are no files named pandas.py, numpy.py or matplotlib.py

Harshal Deore
  • 1,050
  • 1
  • 11
  • 11