导入熊猫在终端不起作用;适用于Python Shell

时间:2016-01-02 07:53:27

标签: python pandas ipython

我在将pandas导入iPython笔记本时遇到了一些问题。但是,当我将pandas导入python shell时,它可以工作。

让我们来看看我的代码:

from __future__ import division
import pandas as pd
import numpy as np

churn_df = pd.read_csv('churn_data.csv')
col_names = churn_df.columns.tolist()

print "Columns names:"
print col_names

to_show = col_names[:6] + col_names[-6:]

print "/nSample data:"
churn_df[to_show.head(6)]

我收到了这个错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-3db7b32fc0b3> in <module>()
      1 from __future__ import division
----> 2 import pandas as pd
      3 import numpy as np
      4 
      5 churn_df = pd.read_csv('churn_data.csv')

/usr/local/lib/python2.7/site-packages/pandas/__init__.pyc in <module>()
     42 import pandas.core.config_init
     43 
---> 44 from pandas.core.api import *
     45 from pandas.sparse.api import *
     46 from pandas.stats.api import *

/usr/local/lib/python2.7/site-packages/pandas/core/api.py in <module>()
      7 from pandas.core.common import isnull, notnull
      8 from pandas.core.categorical import Categorical
----> 9 from pandas.core.groupby import Grouper
     10 from pandas.core.format import set_eng_float_format
     11 from pandas.core.index import Index, CategoricalIndex, Int64Index, Float64Index, MultiIndex

/usr/local/lib/python2.7/site-packages/pandas/core/groupby.py in <module>()
     15 from pandas.core.base import PandasObject
     16 from pandas.core.categorical import Categorical
---> 17 from pandas.core.frame import DataFrame
     18 from pandas.core.generic import NDFrame
     19 from pandas.core.index import Index, MultiIndex, CategoricalIndex, _ensure_index

/usr/local/lib/python2.7/site-packages/pandas/core/frame.py in <module>()
     39                                    create_block_manager_from_arrays,
     40                                    create_block_manager_from_blocks)
---> 41 from pandas.core.series import Series
     42 from pandas.core.categorical import Categorical
     43 import pandas.computation.expressions as expressions

/usr/local/lib/python2.7/site-packages/pandas/core/series.py in <module>()
   2907 # Add plotting methods to Series
   2908 
-> 2909 import pandas.tools.plotting as _gfx
   2910 
   2911 Series.plot = base.AccessorProperty(_gfx.SeriesPlotMethods, _gfx.SeriesPlotMethods)

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.py in <module>()
    133         return False
    134 
--> 135 if _mpl_ge_1_5_0():
    136     # Compat with mp 1.5, which uses cycler.
    137     import cycler

/usr/local/lib/python2.7/site-packages/pandas/tools/plotting.py in _mpl_ge_1_5_0()
    128     try:
    129         import matplotlib
--> 130         return (matplotlib.__version__  >= LooseVersion('1.5')
    131                 or matplotlib.__version__[0] == '0')
    132     except ImportError:

AttributeError: 'module' object has no attribute '__version__'

所以,如果我只是尝试导入pandas:

from __future__ import division
import pandas as pd
import numpy as np

我收到了这个错误:

ImportError                               Traceback (most recent call last)
<ipython-input-2-f13a6d5d1db6> in <module>()
      1 from __future__ import division
----> 2 import pandas as pd
      3 import numpy as np

/usr/local/lib/python2.7/site-packages/pandas/__init__.py in <module>()
     11                       "pandas from the source directory, you may need to run "
     12                       "'python setup.py build_ext --inplace' to build the C "
---> 13                       "extensions first.".format(module))
     14 
     15 from datetime import datetime

ImportError: C extension: hashtable not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace' to build the C extensions first.

然而,当我检查python shell时,我看到了:

me-3:python me$ python
Python 2.7.10 (default, Sep 23 2015, 04:34:21) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>> print pandas.__version__
0.17.1

我因为我做错了而感到茫然。我想这可能是因为我安装了多个版本的python,但我不太清楚这个问题是什么。

由于

1 个答案:

答案 0 :(得分:0)

在笔记本类型中:

In [1]: %ls matplotlib.py

如果您看到列出的文件matplotlib.py,请删除或重命名该文件(使用命令行或文件浏览器等工具)并再次尝试导入。