0

I just want to read a csv file into Pycharm but due to some reason it's not doing it properly.

Here is my code:

import csv
import os
import pandas as pd
path = '/Users/amy/Desktop/data_analysis/additionaldata/df1.csv'
df=pd.read_csv('path', sep=',')

It returned error

Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/harper/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/__init__.py", line 55, in <module>
    from pandas.core.api import (
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/harper/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/core/api.py", line 24, in <module>
    from pandas.core.groupby import Grouper, NamedAgg
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/harper/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/core/groupby/__init__.py", line 1, in <module>
    from pandas.core.groupby.generic import (  # noqa: F401
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 44, in <module>
    from pandas.core.frame import DataFrame
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/core/frame.py", line 114, in <module>
    from pandas.core.series import Series
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/core/series.py", line 84, in <module>
    import pandas.plotting
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/plotting/__init__.py", line 59, in <module>
    from pandas.plotting._core import (
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/plotting/_core.py", line 17, in <module>
    import pandas.plotting._matplotlib  # noqa
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/plotting/_matplotlib/__init__.py", line 3, in <module>
    from pandas.plotting._matplotlib.boxplot import (
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/pandas/plotting/_matplotlib/boxplot.py", line 4, in <module>
    from matplotlib.artist import setp
  File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/Users/amy/PycharmProjects/testing/venv/lib/python3.7/site-packages/matplotlib/__init__.py", line 5, in <module>
    df=pd.read_csv('path', sep=',')
AttributeError: module 'pandas' has no attribute 'read_csv'

I also tried this

import csv
import os
import pandas as pd
path = '/Users/amy/Desktop'
df=pd.read_csv("df1.csv")

which didn't work either.

What's wrong? I have read a few other posts but still not sure how to do this. Any help is appreciated. Thanks

nilsinelabore
  • 4,143
  • 17
  • 65
  • 122
  • You could read it in with vanilla python as well. You ever think of that as well? Also, it looks like the `read_csv` is not under pandas, but a sub module. Is there any submodules that look like `io` or anything? `pd.io.read_csv` or similar? – Fallenreaper Dec 06 '19 at 04:25
  • did you try removing the path and running the code in the working directory? – Gius Dec 06 '19 at 05:25
  • Try checking whether you have given name of a python file same as pandas.This problem was mentioned in various answers(for eg https://stackoverflow.com/questions/43696005/attributeerror-module-pandas-has-no-attribute-read-csv-python3-5) . – Shashank Shekhar Shukla Dec 06 '19 at 06:00
  • Variable `path` is defined in your code but not used, is it expected? – user2235698 Dec 07 '19 at 12:19

1 Answers1

0

Simply add open() to open the file before passing it into pandas.read_csv():

import pandas as pd
path = '/Users/amy/Desktop/data_analysis/additionaldata/df1.csv'
df = pd.read_csv(open(path), sep=',')

(Note: 'path' is only a string with value 'path' if you want the actual path, there shouldn't be any brackets.)

Red
  • 26,798
  • 7
  • 36
  • 58
  • Please have a look at the doc for `pd.read_csv': "pandas.read_csv(filepath_or_buffer: Union[str, pathlib.Path, IO[~ AnyStr]], ..." "Parameters filepath_or_bufferstr, path object or file-like object Any valid string path is acceptable." – Thierry Lathuille Jul 10 '20 at 18:17