[英]Import Pandas Does Not Work In Terminal; Works in Python Shell
將熊貓導入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__'
因此,如果我只是嘗試導入熊貓:
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,但我不太清楚問題是什么。
謝謝
在筆記本中輸入:
In [1]: %ls matplotlib.py
如果您看到列出的文件matplotlib.py
,請刪除或重命名該文件(使用命令行或文件瀏覽器之類的工具),然后再次嘗試導入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.