1

QUESTION : When i try to run "from pandas import read_csv" or "from pandas import DataFrame", I get an error saying "ImportError: cannot import name 'read_csv'" and "[![ImportError: cannot import name 'DataFrame'][1]][1]" respectively.

CODE THAT I AM TRYING TO RUN:

    from pandas import DataFrame
    from sklearn import datasets 
    iris = datasets.load_iris()
    data = pandas.DataFrame(iris)
    kfold = KFold(10, True, 1)
    for train, test in kfold.split(data):
print('train: %s, test: %s' % (data[train], data[test]))

WHAT I TRIED : I checked each and every import error question on stackoverflow and github but I couldn't figure out the solution. I don't have any other file named pandas.py. I tried uninstalling and reinstalling all the packages(like scipy, scikit-learn, numpy, pandas) I tried updating all the packages, but no luck Application specifications that i have - Windows 10, version 1803, Anaconda 4.5.8, spyder 3.3.0

Infact, none of my other code, which was running successfully previously, isn't executing because of these ImportErrors. Any help would be much appreciated. Thanks!

This is the result of "pip show pandas" enter image description here

This is the result of "conda search -f pandas" enter image description here

SSV
  • 149
  • 2
  • 12
  • 3
    if you are importing only "DataFrame" from pandas. you should only be doing: data = DataFrame(iris) and not data = pandas.DataFrame(iris) – dilkash Jul 31 '18 at 08:41
  • [Check out this SO question.](https://stackoverflow.com/questions/36521691/importing-pandas-gives-error-attributeerror-module-pandas-has-no-attribute-c) – Sociopath Jul 31 '18 at 09:31
  • I had checked it long back. I had python version 0.18 and upgraded to 0.22 but now I am getting "AttributeError: module 'pandas' has no attribute 'compat'" error! – SSV Jul 31 '18 at 09:47
  • @Smr how did you install pandas? `conda install pandas` ? – Sleeba Paul Jul 31 '18 at 09:52
  • Yes conda install pandas, and then i did conda update pandas and then i tried pip install pandas==0.22 too – SSV Jul 31 '18 at 09:57

3 Answers3

3

You have already imported DataFrame in statement from pandas import DataFrame.

So you don't need to use pandas.DataFrame, you can just use DataFrame instead.

But my suggestion will be using import pandas as pd, with this you can use all the submodules of pandas.

Use below code:

import pandas as pd
from sklearn import datasets 
iris = datasets.load_iris()
data = pd.DataFrame(iris)
kfold = KFold(10, True, 1)
for train, test in kfold.split(data):
    print('train: %s, test: %s' % (data[train], data[test]))
Sociopath
  • 13,068
  • 19
  • 47
  • 75
1

You know what is wrong? Your file name pandas.py This is funny but a tricky problem no one would easily notice. In fact, when you want to import a library, python first looks into the current folder, then all the python paths defined. Here, you try to import pandas, python first get your pandas.py and look for DataFrame. But there is no DataFrame in it which can be imported.

Change your filename and that's it. In future, don't name your files with standard library names.

Sleeba Paul
  • 613
  • 1
  • 6
  • 14
1

If pandas and sklearn is correctly installed, this should work:

import pandas as pd
from sklearn import datasets 
from sklearn.model_selection import KFold
iris = datasets.load_iris()
data = pd.DataFrame(iris.data,columns = iris.feature_names)
kfold = KFold(10, True, 1)
for train, test in kfold.split(data):
    print('train: %s, test: %s' % (data.iloc[train], data.iloc[test]))
Krishna
  • 6,107
  • 2
  • 40
  • 43
  • I actually cross-checked whether i have installed sklearn and pandas correctly. I even updated those packages. But i still encounter the same "AttributeError: module 'pandas' has no attribute 'core'" error – SSV Jul 31 '18 at 09:18
  • Which pandas version have you installed? check [this](https://stackoverflow.com/q/36521691/7699859) @Smr – Krishna Jul 31 '18 at 09:19
  • I have pandas (0.18.1) – SSV Jul 31 '18 at 09:35