AttributeError:“系列”对象没有属性“管道”

时间:2018-08-03 04:50:10

标签: pandas numpy scipy scikit-learn keras

在ec2实例上运行python3 keras代码时,出现以下错误。该代码在Azure Jupyter Notebook上运行良好。

代码:

numpy.random.seed(7)
dataframe = pandas.read_csv("some_data.csv", header = None)
df = dataframe
char_cols = df.dtypes.pipe(lambda x: x[x == 'object']).index
for c in char_cols:
    df[c] = pandas.factorize(df[c])[0]
dataframe = df

错误:

Traceback (most recent call last):
      File "pi_8_1st_year.py", line 12, in <module>
        char_cols = df.dtypes.pipe(lambda x: x[x == 'object']).index
      File "/usr/lib/python3/dist-packages/pandas/core/generic.py", line 1815, in __getattr__
        (type(self).__name__, name))
    AttributeError: 'Series' object has no attribute 'pipe'

我的配置:

ubuntu@ipxxxx:~$ python3
Python 3.4.3 (default, Nov 28 2017, 16:41:13) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
_frozen_importlib:321: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
/usr/lib/python3.4/importlib/_bootstrap.py:321: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
  return f(*args, **kwds)
>>> pandas.__version__
'0.13.1'
>>> import numpy
>>> numpy.__version__
'1.15.0'
>>> import sklearn
/usr/lib/python3.4/importlib/_bootstrap.py:321: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
  return f(*args, **kwds)
/usr/lib/python3.4/importlib/_bootstrap.py:321: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
  return f(*args, **kwds)
>>> sklearn.__version__
'0.19.2'
>>> import keras
Using TensorFlow backend.
/usr/lib/python3.4/importlib/_bootstrap.py:321: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
  return f(*args, **kwds)
>>> keras.__version__
'2.2.2'
>>> 

1 个答案:

答案 0 :(得分:0)

错误提示您不能在系列对象上使用pipedf.dtypes返回一个序列对象,因此返回错误。 如果要查找对象类型的列,可以通过以下方式实现:

s = (df.dtypes == 'object')
cols = s[s.values == True].index